PHP form - Why validation doesn´t work? - php

I´m trying to validate a simple PHP form with HTML code but it doesn´t work properly.
Here you are HTML code:
<form action="expresscontactform.php" method="post" name="form1" id="form1">
<label>Name </label><input id="Name" name="Name" type="text" value="<?php echo $_POST['Name']; ?>">
<span class="error"> <?php echo $errors[1]; ?> </span>
<label>Email </label><input id="Email" name="Email" type="text" value="<?php echo $_POST['Email']; ?>">
<span class="error"> <?php echo $errors[4]; ?> </span>
<label>Phone </label><input id="Phone" name="Phone" type="text" value="<?php echo $_POST['Phone']; ?>">
<span class="error"> <?php echo $errors[2]; ?> </span>
<label>Country of origin</label><input id="Country" name="Country" type="text" value="<?php echo $_POST['Country']; ?>">
<span class="error"> <?php echo $errors[3]; ?> </span>
<label>Message </label><textarea id="message" cols="5" rows="5" name="Message"></textarea>
<input value="Send" class="send_request_new" type="submit">
</form>
And this is PHP code (on the server):
<?php
if(isset($_POST['send_request_new'])){
$errors = array();
if($_POST['Name'] == ''){
$errors[1] = '<span class="error">Please type your name</span>';
}else if($_POST['Phone'] == ''){
$errors[2] = '<span class="error">Please type your phone number</span>';
}else if($_POST['Country'] == ''){
$errors[3] = '<span class="error">Please type your country</span>';
}else{
$EmailFrom = Trim(stripslashes($_POST['Email']));
$EmailTo = 'webmaster#theacademy.co,' . $EmailFrom;
$Subject = "Online Application Form";
$name = Trim(stripslashes($_POST['Name']));
$phone = $_POST['Phone'];
$country = $_POST['Country'];
$message = $_POST['Message'];
$header = 'From: ' . $EmailFrom . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
// prepare email body text
$Body .= "Contact form";
$Body .= "\n";
$Body .= "\n";
$Body .= "This is an automatically generated e-mail, to inform you that we received your request. We will contact you as soon as possible.";
$Body .= "\n";
$Body .= "\n";
$Body .= "Kind regards";
$Body .= "\n";
$Body .= "\n";
$Body .= "**********************************************";
$Body .= "\n";
$Body .= "\n";
$Body .= "The Academy";
$Body .= "\n";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "Your Request:";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Country: ";
$Body .= $country;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $EmailFrom;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
$Body .= "\n";
$Body .= "Sent on " . date('d/m/Y', time());
$Body .= "\n";
$Body .= "\n";
$Body .= "Last visited page: ";
$Body .= $_SERVER['HTTP_REFERER'];
if(mail($EmailTo, $Subject, $Body, $header)){
$result = '<div class="result_ok">Email sent successfully</div>';
// If successfully we reset all the fields
$_POST['nombre'] = '';
$_POST['email'] = '';
$_POST['asunto'] = '';
$_POST['mensaje'] = '';
header("refresh:3;url=http://www.myexample.co/");
}else{
$result = '<div class="result_fail">Error!!</div>';
}
}
}
?>
I think there is a mistake in the line below:
<span class="error"> <?php echo $errors[1]; ?> </span>
But I don´t know where exactly.
One more question, how can I set "The Academy" as email sender?
I don´t know if this is the best way to validate a form, if someone show me other way I will be thankful to learn.
I appreciate a lot if anyone can help me, please.
Thanks in advance.

Firstly, your code's execution is dependant on this conditional statement if(isset($_POST['send_request_new'])) where it's looking for a "named" element called send_request_new therefore would never execute.
Your present (unnamed) submit button
<input value="Send" class="send_request_new" type="submit">
this should be changed to:
<input value="Send" class="send_request_new" type="submit" name="send_request_new">
you have a class named that way, instead of a name.
In order to get it to work, you would need to place your entire HTML/PHP inside the same page and use action=""
I noticed you don't have one for your Email and the message, only for the name/phone number and country.
Plus, to use a personalized method for the sender's name:
Base yourself on the following:
$Name = "The Academy";
$email = "email#example.com";
$header = "From: ". $Name . " <" . $email . ">\r\n";
You can use some of the filters on PHP.net to validate and protect against XSS injection:
-http://php.net/manual/en/filter.filters.validate.php
One of which being FILTER_VALIDATE_EMAIL

Related

My contact form worked fine, until I added attachment option. Messages no longer get sent. Any idea how I could validate the attachment field?

I have made a contact form with the following fields: Name, Email, Message. It all worked fine - messages were sent to my email - until I added the attachments option to the form.
I've tried validating the attachment fields by searching up tutorials, but nothing seems to work. I guess I'm just not sure how to implement it to my already existing code.. Any help here?
Here is the form:
<?php include 'contact-form.php'; ?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<h3>Contact Us</h3>
<fieldset>
<input placeholder="Nimi" type="text" tabindex="1" name="thename" value="<?= $thename ?>" autofocus>
<div class="error"><span><?= $name_error ?></span></div>
</fieldset>
<fieldset>
<input placeholder="Email" type="text" tabindex="2" name="email" value="<?= $email ?>">
<div class="error"><span><?= $email_error ?></span></div>
</fieldset>
<fieldset>
<textarea placeholder="Sisesta sõnum siia.." type="text" tabindex="3" name="message"></textarea>
<div class="error"><span><?= $message_error ?></span></div>
</fieldset>
<fieldset>
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<div class="error"><span><?= $attachment_error ?></span></div>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Saatmine">Saada</button>
</fieldset>
<div class="success"><?= $success; ?></div>
<div class="error"><?= $error; ?></div>
</form>
Here is PHP validation code contact-form.php:
<?php
$name_error = $email_error = $message_error = $attachment_error = "";
$thename = $email = $message = $success = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["thename"])) {
$name_error = "Palun sisesta nimi";
} else {
$thename = test_input($_POST["thename"]);
// check if name only contains letters, whitespace and hyphen
if (!preg_match("/^[a-zA-Z -]*$/",$thename)) {
$name_error = "Sisestada saab ainult tähti, tühikuid ja sidekriipse";
}
}
if (empty($_POST["email"])) {
$email_error = "Palun sisesta email";
} else {
$email = test_input($_POST["email"]);
// email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Sisesta email korrektselt";
}
}
if (empty($_POST["message"])) {
$message_error = "Palun sisesta sõnum";
} else {
$message = test_input($_POST["message"]);
}
if (empty($_FILES["attachment"])) {
$attachment_error = "Palun sisesta enda eluloo fail";
}
if ($name_error == '' and $email_error == '' and $message_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
$to = 'myemail#gmail.com';
$subject = 'Eesti Elulood';
$message = "Sulle saadeti kiri Rannu koguduse kodulehelt.\n\nSaatja nimi: $thename\n\nSaatja email: $email\n\nSõnum: $message";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' . "
<$email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' .
" <$email>" . PHP_EOL;
$headers .= "Return-Path: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL .
PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++)
{
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file =
chunk_split(base64_encode(file_get_contents($_FILES['attachment']
['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"
{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL .
$data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=',
$datamsg, $headers, "-f$email")){
$success = "Thankyou, message sent!.";
} else {
$error = "Sorry but the email could not be sent. Please try again!";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
After hitting the submit button, It just takes me to the index.php page..
Any help is appreciated!
1) You have no mail attachments code into your php code except for the html markup, so you cannot send your mail attachments.
2) You have to encode the attachments using chunk_split(base64_encode()) and then you have to import them into your message part using the correct way.
3) You forgot to enter the correct headers, that's the other reason why you can't send your mails.
4) You have to consider that if you use GMail there may be a limit to the type of file you can send and so read this: https://support.google.com/mail/answer/6590?hl=en
5) I suggest you to use the long php tag instead the short tag:
Instead of writing <?= $_SERVER['PHP_SELF']; ?>, write <?php echo $_SERVER['PHP_SELF']; ?>
6) You have a serious error into your php and this is the reason why pressing submit you are in the home instead of your contact form:
<?= $SERVER['PHP_SELF']; ?> is wrong!
<?= $_SERVER['PHP_SELF']; ?> is correct!
See you point 5)
Here is an example of correct html markup for attachments:
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<label for="attachment3">File:</label> <input type="file" id="attachment3" name="attachment[]" size="35">
Here is an example of correct php mail code for attachments:
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Return-Path: $from_email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL . PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++) {
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL . $data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($recipient_email, '=?UTF-8?B?' . base64_encode($subject) . '?=', $datamsg, $headers, "-f$from_email")) {
exit("Files Sent Successfully");
} else {
exit("Sorry but the email could not be sent. Please go back and try again!");
}
Where $sender_name is the name of sender, $from_email is the email of sender, $recipient_email is the recipient of your email.
You can take an example from my code and integrate it into your project, I wrote only the essential parts concerning the sending of attachments.
I hope this helps.

Email field is empty on PhpMail

I have a simple form which sending an email with some fields and also it saves the fields in .csv file.
Here is my code (HTML, jQuery, PHP)
The Form:
<form id="request-size">
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
<input type="hidden" name="size" id="size" value="52">
<input type="hidden" name="sku" id="sku" value="315">
<input type="hidden" name="pridpr" id="pridpr" value="849">
<input class="request-size-btn btn" value="SEND">
</form>
The jQuery:
jQuery('.request-size-btn').live('click', function() {
jQuery.ajax({
type: "POST",
url: "https://example.com/sendmail.php",
data: jQuery('form#request-size').serializeArray(),
success: function( data ) {
jQuery('.cmesg').empty();
jQuery('.cmesg').append(data);
}
});
});
});
The PHP Sendmail:
$EmailFrom = $_REQUEST['email'];
$SKU = $_REQUEST['sku'];
$SIZE = $_REQUEST['size'];
$ID = $_REQUEST['pridpr'];
$EmailTo = "info#example.com";
$Subject = "Contact for Size";
$DATENOW = date('d/m/Y h:i:s a', time());
$Body = "";
$Body .= "<b>Email:</b> ";
$Body .= $EmailFrom . "\r\n";
$Body .= "<br>";
$Body .= "<b>Product SKU:</b> ";
$Body .= $SKU . "\n";
$Body .= "<br>";
$Body .= "<b>Product ID:</b> ";
$Body .= $ID . "\n";
$Body .= "<br>";
$Body .= "<b>Product Size:</b> ";
$Body .= $SIZE;
$Body .= "<br>";
$Body .= "<b>Date:</b> ";
$Body .= $DATENOW;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: ". $EmailFrom ."" . "\n";
$success = mail($EmailTo, $Subject, $Body, $headers);
if ($success){
echo '<div class="alert alert-success">Done!</div>';
$file = fopen('request-size.csv', 'a');
$data = array(
array($EmailFrom, $SKU, $ID, $SIZE, $DATENOW)
);
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
}
else{
echo '<div class="alert alert-danger">Error!</div>';
}
I've got all the fields in my csv file and email, except the "email" field. It's always empty. I have also try to change the name of email field (email to emailreq) on both sides (html, php), but didn't work.
Any suggestions? Thank you!
If updating jQuery is an option for you, do that. Version 1.6.2 and up should be working (possibly also before, but i'm not sure).
Otherwise change the
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
to
<input name="email" id="email" type="text" class="cmesgmail" placeholder="email">
Older versions of jQuery did not include support for the HTML5 input types.

Trouble sending email through contact form with php [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I've never worked with php before and am trying to make my contact form functional with the help of some php code I acquired through google.
First off do I need to link it to my html page somehow (like css/ javascript) or does it work fine if it shares the same folder?
I have modified the names of certain parts to match the names in my html but am unable to get it to work. When I click the submit button I get a page saying...
"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Deadline: "; $Body .= $Dealine; $Body .= "\n"; $Body .= "Interested: "; $Body .= $Interested; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?>
The php code is as follows.
<?php
$EmailFrom = "c#c.co.nz";
$EmailTo = "c#c.co.nz";
$Subject = "contact-form";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Email = Trim(stripslashes($_POST['Email']));
$Deadline = Trim(stripslashes($_POST['Dealine']));
$Interested = Trim(stripslashes($_POST['Interested']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Deadline: ";
$Body .= $Dealine;
$Body .= "\n";
$Body .= "Interested: ";
$Body .= $Interested;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact-thanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Here is my html
<div role="form" class="form-wrapper">
<form name="contact-f" action="contact-form.php" method="post">
<div class="f-width">
<div class="f-col-1 f-group-1"><input type="text" name="Name" class="c-back input-t f-col-1" placeholder="Name" /></div>
<div class="f-col-1 f-group-2"><input type="email" name="Company" class="c-back input-t f-col-1" placeholder="Company Name" /></div>
<div class="f-col-1 f-group-1"><input type="text" name="Email" class="c-back input-t f-col-1" placeholder="Email Address" /></div>
<div class="f-col-1 f-group-2"><select name="Deadline" class="c-back input-t f-col-1"><option class="opt-f">Do you have a Deadline?</option><option value="Not yet" class="opt-f">Not yet</option><option value="Less than 1 Month" class="opt-f">Less than 1 Month</option><option value="2-3 Months" class="opt-f">2-3 Months</option><option value="3-6 Months" class="opt-f">3-6 Months</option><option value="6+ Months" class="opt-f">6+ Months</option></select></div>
<div class="f-col-2 f-group-3"><select name="Interested" class="c-back input-t f-col-2"><option class="opt-f">What are you interested in?</option><option value="Branding" class="opt-f">Branding</option><option value="Print Design" class="opt-f">Print Design</option><option value="Illustration" class="opt-f">Illustration</option><option value="Website / UI Design" class="opt-f">Website / UI Design</option><option value="Literature" class="opt-f">Literature</option><option value="Video Editing" class="opt-f">Video Editing</option><option value="Other" class="opt-f">Other</option></select></div>
<div class="f-col-3 f-group-4"><textarea name="Message" class="c-back input-t message-f f-col-3" placeholder="Describe your project..."></textarea></div>
<div class="f-submit f-group-5"><input type="Submit" name="Submit" value="Send" class="form-btn" /></div>
</div></form></div>
(And yes I have created the contact-thanks.php file it calls for at the end).
So many $Body , How About :
$Body = " Name : $Name \n Company : $Company \n Deadline : $Deadline \n Interested : $Interested \n Message : $Message";
Is This What You Looking For ?

How do I paste a image on a email message

Oke, this is a tricky one. (I think).
Do you know when you receive a message and the email has a image on top or on the bottom of the page.
I have been trying to make it but can't figure it out.
I hope you guy's can help me with this problem.
This is what I got so far;
<?php
$to = 'info#gmail.com';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$gast = $_POST['email'];
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if(empty($errors))
{
$to = $to;
$from = $gast;
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
$body = "E-mail".
"Name: $name\n".
"Email: $gast \n".
"Content-Type: {$fileType};\n".
"Content-Transfer-Encoding: base64\n\n".
$data;
$headers = "From: $from \r\n";
$headers .= "Email: $gast \r\n";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= " boundary=\"{$mimeBoundary}\"";
$headers .= "img src='http://cache.estivant.nl/image/1399025430_12_banners-bestemmingen-single-1680x460-extra2-06-kos_1399025430.jpg' alt='image'";
$data = chunk_split(base64_encode($data));
mail($to, $body, $headers);
}
}
?>
<html>
<head></head>
<body>
<form method="post" action="">
<label for="name">name</label>
<input type="name" name="name" value="" />
<label for="email">email</label>
<input type="email" name="email" value="" />
<button id="submit" name="submit">send</button>
</form>
</body>
</html>
Simply write a html page with css styles and format your email accordingly and use the html as your email body. This will create an image in the mail
<img src="image.jpg" alt="image"></img>
Please be aware that most (if not all) email clients will block your image from being showed to your receiver as long as you are not in their trusted sender list or contact list.
Of course, your email has to be sent as html
Here is an example of a nicely formatted email with images.
Note image syntax: <img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />'
$to = 'bob#example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['req-name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['req-email']) . "</td></tr>";
$message .= "<tr><td><strong>Type of Change:</strong> </td><td>" . strip_tags($_POST['typeOfChange']) . "</td></tr>";
$message .= "<tr><td><strong>Urgency:</strong> </td><td>" . strip_tags($_POST['urgency']) . "</td></tr>";
$message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>" . $_POST['URL-main'] . "</td></tr>";
$addURLS = $_POST['addURLS'];
if (($addURLS) != '') {
$message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . strip_tags($addURLS) . "</td></tr>";
}
$curText = htmlentities($_POST['curText']);
if (($curText) != '') {
$message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
}
$message .= "<tr><td><strong>NEW Content:</strong> </td><td>" . htmlentities($_POST['newText']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
Code source and full explanation: http://css-tricks.com/sending-nice-html-email-with-php/

Only 2 out of 9 PHP form fields being sent to email

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 .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: ";
$Body .= $Street;
$Body .= "\n";
$Body = "Town: ";
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "\n";
$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 .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: "; //needs dot
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: "; //needs dot
$Body .= $Street;
$Body .= "\n";
$Body = "Town: "; //needs dot
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: "; //needs dot
$Body .= $Postcode;
$Body .= "\n";
$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 .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: ";
$Body .= $Street;
$Body .= "\n";
$Body = "Town: ";
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "\n";
$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!

Categories