I've got a pretty simple contact form (or so I thought), but only 2 out of 9 contact fields are being delivered to my inbox. Strangely enough, it's the last two fields in the list, although the 'from' email address, and subject matter are working fine.
<form id="quote" class="contact_form" action="contact.php" method="post">
<h2>Easy Contact Form</h2>
<h4>Personal</h4>
<label for="title">Title:</label>
<input type="text" name="title" id="title" required class="required" >
<label for="name">Full Name:</label>
<input type="text" name="fullname" id="fullname" required class="required" >
<label for="email">Email:</label>
<input type="email" name="email" id="email" required placeholder="jsmith@email.com" class="required email">
<label>Phone:</label>
<input type="text" name="phone" id="phone" />
<h4>Address</h4>
<label>House #:</label>
<input type="text" name="house" id="house" />
<label>Street:</label>
<input type="text" name="street" id="street" />
<label>Town/City:</label>
<input type="text" name="town" id="town" />
<label>Postcode:</label>
<input type="text" name="postcode" id="postcode" />
<h4>Lender</h4>
<label>Company Name:</label>
<input type="text" name="companyname" id="companyname" />
<input class="btn" type="image" src="images/submit_btn.jpg"/>
</form>
And here's the PHP:
<?php
$EmailFrom = $_REQUEST['email'];
$EmailTo = "me@email.com";
$Subject = "Information";
$Title = Trim(stripslashes($_POST['title']));
$Fullname = Trim(stripslashes($_POST['fullname']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['phone']));
$House = Trim(stripslashes($_POST['house']));
$Street = Trim(stripslashes($_POST['street']));
$Town = Trim(stripslashes($_POST['town']));
$Postcode = Trim(stripslashes($_POST['postcode']));
$Companyname = Trim(stripslashes($_POST['companyname']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "Error";
exit;
}
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "
";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "
";
$Body = "house: ";
$Body .= $house;
$Body .= "
";
$Body = "Street: ";
$Body .= $Street;
$Body .= "
";
$Body = "Town: ";
$Body .= $Town;
$Body .= "
";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "
";
$Body .= "Company Name: ";
$Body .= $Companyname;
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
echo "Succes";
}
else{
echo "Error";
}
?>
Here's the problem:
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "
";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body = "Phone: "; //needs dot
$Body .= $Phone;
$Body .= "
";
$Body = "house: ";
$Body .= $house;
$Body .= "
";
$Body = "Street: "; //needs dot
$Body .= $Street;
$Body .= "
";
$Body = "Town: "; //needs dot
$Body .= $Town;
$Body .= "
";
$Body = "Postcode: "; //needs dot
$Body .= $Postcode;
$Body .= "
";
$Body .= "Company Name: ";
$Body .= $Companyname;
You need to make sure each line has .=
instead of just =
. Near the bottom where you have $Body = "Postcode: ";
, you're resetting the entire $Body variable.
The problem is from $body
.
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "
";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "
";
$Body = "house: ";
$Body .= $house;
$Body .= "
";
$Body = "Street: ";
$Body .= $Street;
$Body .= "
";
$Body = "Town: ";
$Body .= $Town;
$Body .= "
";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "
";
$Body .= "Company Name: ";
$Body .= $Companyname;
The first variable declaration $Body = "Title: ";
is fine, however after that you need to make sure you are appending to the string using .=
.
Each time after the intital declaration you use =
, it clears everything assigned or appended previously and starts again.
This is why you only see the last 2 form fields, because $Body = "Postcode: ";
restarts the variable, removing everything before.
Use .=
for every single line except the first one, which should be an =
.
Hope this helps!