Php variable in contact form / message body - php

I have a contact form that is only passing over some of the form fields and i wondered if anyone can help as to why?
The html form is as follows:
<form enctype="multipart/form-data" method="post" action="referral.php" onsubmit="return submitForm()">
<fieldset>
<div class="field">
<input name="name" id="name" type="text" class="validate" placeholder="Your Name" />
<span class="form-msg" id="name-error">Please add your name</span>
</div>
<div class="field here-for-next-until">
<input name="email" id="email" type="text" class="validate" placeholder="Your E-mail" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your e-mail address</span>
</div>
<div class="field">
<input name="phone" id="phone" type="text" placeholder="Your Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="message" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<input type="text" id="cap_val" name="cap_val" class="code validate" placeholder="Enter Code From Right"/>
<img src="bin/captcha.php" id="cap"/><img src="images/refresh.jpg" id="cap-refresh" onclick="change_captcha()"/><br/>
<span id="captcha-error" class="form-msg" >Please add the above code</span>
<div class="clear here-for-next-until"></div>
</div>
</div>
<div class="flt-lt w420">
<h2 class="subheader">Client</h2>
<div class="field">
<input name="referralsname" id="name" type="text" class="validate" placeholder="Name" />
<span class="form-msg" id="name-error">Please add your referrals name</span>
</div>
<div class="field here-for-next-until">
<input name="referralsemail" id="email" type="text" class="validate" placeholder="E-mail address" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your referrals e-mail address</span>
</div>
<div class="field">
<input name="referralsphone" id="phone" type="text" placeholder="Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your referrals phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="referralsmessage" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<div class="clear here-for-next-until"></div>
<div id="submit-holder">
<input class="submit" type="submit" value="Submit"/>
<input id="reset" class="submit" type="reset" value="Reset"/>
</div>
</div>
</fieldset>
</form>
Basically this is two very similar contact forms side by side that share a submit button
When posted it goes to this:
$owner_email = 'myemail.co.uk';
$subject = A message from your site visitor ' . $_POST["name"];
$messageBody = "\nVisitor: " . $_POST["name"] . "\n";
$messageBody .= "Email Address: " . $_POST['email'] . "\n";
$messageBody .= "Phone Number: " . $_POST['phone'] . "\n";
$messageBody .= "Message: " . $_POST['message'] . "\n";
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
$messageBody .= "Email Address: " . $_POST['referralsemail'] . "\n";
$messageBody .= "Phone Number: " . $_POST['referralsphone'] . "\n";
$messageBody .= "Message: " . $_POST['referralsmessage'] . "\n";
However when I submit the form I onyl get one set of details (the first 4 $messageBody)
Any help anyone could offer would be greatly appreciated.
Thanks again
David

Change:
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
To:
$messageBody .= "\nVisitor: " . $_POST["referralsname"] . "\n";
(.= instead of =, you are re-assigning value to $messageBody instead of chaining it to $messageBody)

if i had to guess, i would say it is because you are repeating the IDs. In HTML element IDs have to be unique. make all IDs match your "name" values. im referring to
<input name="phone" id="phone"....
<input name="referralsphone" id="phone"....
change all these cases to:
<input name="phone" id="phone"....
<input name="referralsphone" id="referralsphone"....
I would also suggest checking the contents of POST. this question shows how Echo an $_POST in PHP

Related

My form submission not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
HTML Codes
<h3>online consultation</h3>
<h5>We have provided contact information down below. You can contact us either by mobile, via email or via contact form. We will at once contact you to discuss your problems! If you have special requirments a consultation can be decided.</h5>
</div>
<div class="col-md-7 col-lg-offset-3 contact-grid-right">
<form method="post" action="mail.php">
<input type="text" placeholder="Your Name" name="name" id="name" required="required">
<input type="text" placeholder="Email" name="Email" id="Email" required="required">
<input type="text" placeholder="Contact No" name="contact" id="contact" required="required">
<input type="text" placeholder="Subject" name="subject" id="subject" required="required">
<textarea rows="2" cols="70" type="text" placeholder="MESSAGE" name="message" id="message" required="required">
</textarea>
<button type="submit" class="slide-btn"> Send your message <i class="fa fa-paper-plane" aria-hidden="true"></i> <br></button>
</form>
</div>
PHP Script
<?php
$n=$_POST['name'];
$mail=$_POST['email'];
$ph=$_POST['contact'];
$ln=$_POST['subject'];
$msg=$_POST['message'];
$to="email#yahoo.com";
$sub="message";
$body="
<html>
<body>
<p><b></b></p><h4></h4>
<table border='0'>
<tr>
<b>Name : </b> $n <br><br>
<b>Email : </b> $mail <br><br>
<b>Contact :</b> $ph <br><br>
<b>Subject :</b> $ln <br><br>
<hr>
<b>Contact Form Message:</b> <br><br>$msg
</tr>
</table>
</body>
</html>";
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8". "\r\n";
$headers .= 'From:'.$mail . "\r\n";
//$headers .= 'Cc: email#yahoo.com.au'; "\r\n";
//$headers .= 'Bcc: '; "\r\n";
mail($to,$sub,$body,$headers);
//echo "<script>alert('Message Send Sucessfully')</script>";
echo "<script>window.location.href='index.html'</script>";
?>
here the solution change the $mail=$_POST['email']; in your php script to $mail=$_POST['Email'];
For keeping your code standard replace the email html input for:
<input type="text" placeholder="Email" name="email" id="email" required="required">

My php email form is not returning the data entered by the user

As stated in the title, my webform is not returning the user information in the email, although the field labels are being returned.
I think this is an issue where the HTML is not structured to relate back to the php field name, but I am not sure how to format that.
Can anyone set me on the path?
This is the HTML:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
</form>
And this is the php:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'Example#email.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Firstly, your inputs does not have name attributes:
<input type="text" class="input-block-level" required="required" name="first_name" placeholder="Your First Name">
<input type="text" class="input-block-level" required="required" name="last_name" placeholder="Your Last Name">
<input type="text" class="input-block-level" required="required" name="email" placeholder="Your email address">
Secondly, you need to use <input> instead of <button>. Otherwise, your form will not be submitted.
<input type="submit" class="btn btn-primary btn-large pull-right">Send Message</input>
Thirdly, you need to check if the form is submitted by using isset() function:
// will return true if the form is submitted
if(isset($_POST['submit']){
header('Content-type: application/json');
// other parts of your code
}
Lastly, you shouldn't suppress the error messages by removing the # as they are very useful in helping you to debug your errors, especially when you have a syntax error.
You need to give your inputs a name attribute, such as:
<input type="text" name="name" class="input-block-level" required="required" placeholder="Your First Name" />
Then you will be able to access the $_POST data as you currently are. Check out the name attribute docs

if isset Not Writing To Database

I have an if isset statement that fires when my site receives approval from my merchant account api after a form is submitted. Everything seems to work except for the order details from the form getting written to my database. Here is the code for my form:
<form id="input-form" class="form-inline" action="" method="post">
<?php
if($error <> '') {
echo '<div class="alert alert-danger">',$error,'</div>';
}
?>
<div id="makepayment">
<div class="form-group">
<label for="order_fname">First Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_fname" id="order_fname" value="<?php echo $user['user_fname']; ?>" placeholder="First Name" />
</div>
<div class="form-group">
<label for="order_lname">Last Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_lname" id="order_lname" value="<?php echo $user['user_lname']; ?>" placeholder="Last Name" />
</div>
<div class="form-group">
<label for="order_phone">Primary Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_phone" id="order_phone" value="<?php echo $user['user_phone']; ?>" placeholder="Phone Number" />
</div>
<div class="form-group">
<label for="order_cell">Cell Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_cell" id="order_cell" value="<?php echo $user['user_cell']; ?>" placeholder="Cell Phone" />
</div>
<div class="form-group">
<label for="order_email">Email</label>
<input class="form-control" style="width: 273px;" type="text" name="order_email" id="order_email" value="<?php echo $user['user_email']; ?>" placeholder="Email Address" />
</div><br/>
<div class="clear"></div>
<div class="form-group" style="width:100% !important">
<label for="company_id">Property Information</label><br/>
<br/>
<input id="company_id" type="hidden" id="company_id" name="company_id" value="<?php echo $company['company_id']; ?>" />
<input id="company_name" type="hidden" id="company_name" name="company_name" value="<?php echo $company['company_name']; ?>" />
<div class="property">
<strong><?php echo $company['company_name']; ?></strong><br/>
<?php echo $company['company_street']; ?><br/>
<?php echo $company['company_city']; ?>, <?php echo $company['company_state']; ?> <?php echo $company['company_zip']; ?>
</div>
<div class="clear"></div>
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_cc">Card Number</label>
<input class="form-control" type="text" name="ssl_card_number" id="ssl_card_number" style="width:100% !important" />
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_code">Security Code</label>
<input class="form-control" style="width: 75px;" type="text" name="order_code" id="order_code" placeholder="ex:123" />
</div>
<div class="form-group">
<label for="order_customernumber">Customer Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_customernumber" id="order_customernumber" />
</div>
<div class="form-group">
<label for="order_invoicenumber">Invoice Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_invoicenumber" id="order_invoicenumber" />
</div>
<div class="form-group">
<label for="order_amount">Payment Amount</label>
$ <input class="form-control" type="text" style="width: 100px" name="ssl_amount" id="ssl_amount" />
</div>
<div class="form-group" style="width:100% !important">
<input class="btn btn-primary" type="submit" name="submit" value="Submit" />
</div>
</div>
</form>
Here is my if isset statement that fires if the api sends back an approval code. I know that part works because it does this action when I put in my card info and throws the proper error when I put in fake info.
if(isset($response['ssl_result_message']) && $response['ssl_result_message'] == "APPROVAL") {
//If transaction successful write to database
$userid = $session['user_id'];
$addOrderSQL = "
INSERT INTO orders (
user_id,
company_id,
order_fname,
order_lname,
order_phone,
order_cell,
order_email,
company_name,
order_customernumber,
order_invoicenumber,
order_amount
)
VALUES (
'$userid',
'$_POST[company_id]',
'$_POST[order_fname]',
'$_POST[order_lname]',
'$_POST[order_phone]',
'$_POST[order_cell]',
'$_POST[order_email]',
'$_POST[company_name]',
'$_POST[order_customernumber]',
'$_POST[order_invoicenumber]',
'$_POST[ssl_amount]'
)";
$firstname= $_POST['order_fname'];
$lastname = $_POST['order_lname'];
$invoice = $_POST['order_invoicenumber'];
$customernum = $_POST['order_customernumber'];
$property = $_POST['company_name'];
$ordertotal = $_POST['ssl_amount'];
/* -------------------------------------------------
Send Email to Administrator
------------------------------------------------- */
$to = "web#abcprintingink.com";
$subject = "Payment for Customer # ". $customernum;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: webmaster#turfmastersct.com" . "\r\n" .
"Reply-To: webmaster#example.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$message = "<p>A customer has completed a payment. Here are the payment details:</p>
Customer Name: ". $firstname ." ". $lastname ."<br/>
Customer #: ". $customernum ."<br/>
Invoice #: ". $invoice ."<br/>
Property: ". $property ."<br/>
Payment Amount: $". $ordertotal;
// Send it.
mail($to, $subject, $message, $headers);
//once executed queries redirect user
header('Location: /payments/orders.php');
exit;
}
elseif(isset($response['errorCode'])) {
$error = 'Your credit card has been declined';
}
else {
$error = 'Failed to process your payment';
}
The form is communicating with the api, I get a response from it and I can see the payment details post to my demo account. The isset statement fires, I get the email that it sends and it redirects me to the orders.php page like it's supposed to. It just doesn't write to the database. You can see my form and you can see the fields I am writing to my database. All those fields exist in the database itself. Am I missing something here?
You never run the $addOrderSQL query.
See mysqli_query() to do this.

I want to echo message when form is submitted

I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name#email.com", $subject, $message, $from);
}
?>
Change: (and using a conditional statement)
mail("name#email.com", $subject, $message, $from);
to:
if(mail("name#email.com", $subject, $message, $from)){
$sent = "Message sent";
}
Then modify:
<div class="sent">Email has been sent.</div>
to read as:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.
http://php.net/manual/en/function.empty.php
Set inside a conditional statement also.
I.e.: using NOT empty: ! is the NOT operator in PHP.
if(!empty($_POST['var'])){...}
Edit:
The logic needs to be reversed.
First, place your PHP above your HTML form.
I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email#example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>
$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>

php form not working

I cant get this to work properly. I know its something stupid but I cant find it.
<?php
$mailto = "stormygurl73#yahoo.com";
$name = ucwords($_POST['name']);
$subject = $_POST['Contact form'];
$email = $_POST['email'];
$message = $_POST['message'];
if(strlen($_POST['name']) < 1 ){
echo 'email_error';
}
else if(strlen($email) < 1 ) {
echo 'email_error';
}
else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", $email)) {
echo 'email_error';
}
else if(strlen($message) < 1 ){
echo 'email_error';
} else {
// NOW SEND THE ENQUIRY
$email_message="\n\n" .
"Name : " .
ucwords($name) .
"\n" .
"Email : " .
$email .
"\n\n" .
"Message : " .
"\n" .
$message .
"\n" .
"\n\n" ;
$email_message = trim(stripslashes($email_message));
mail($mailto, $subject, $email_message, "From: \"$vname\" <".$email.">\nReply-To: \"".ucwords($name)."\" <".$email.">\nX-Mailer: PHP/" . phpversion() );
}
?>
Html
<div class="wrapper">
<div id="main" style="padding:50px 0 0 0;">
<form id="contact-form" action="sendemail.php" method="post">
<h3>Get in touch</h3>
<h4>Fill in the form below, and we'll get back to you within 24 hours.</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
<!-- /Form -->
Any help would be awesome!
You must have the name attribute on your input-elements. The first one should have
name="name"
and so on. You can read more about it here: Variables From External Sources : HTML Forms (GET and POST)
It looks like you've missed the attribute name name='somename' everywhere in your form fields. e.g.
This is what you have
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
This is what it should have been
<input name="name" placeholder="Please enter your name" type="text" tabindex="1" required autofocus>

Categories