My PHP Contact from is sending Blank messages to me - php

HTML:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
</li>
<li>
<label for="name">Email*</label>
<input type="email" name="email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="name">Website</label>
<input type="url" name="website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea rows="6" cols="40" name="message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
HTML
/* Subject and Email Variables */
$emailSubject = 'Contact Form';
$webMaster = 'email#address.com';
/* Gathering Data Variables */
$nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message'];
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered as HTML */

There are more problems, so:
you missed name attribute at the name text input
you used incorect names, PHP is case-sensitive in keys ($_POST['name'] !== $_POST['Name']). This is the point why you received blank meesages
for attribute in labels has to correspond with input ID
Updated code:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="Name">Name*</label>
<input type="text" id="Name" name="Name" placeholder="Your Name" required />
</li>
<li>
<label for="Email">Email*</label>
<input type="email" name="Email" id="Email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="Website">Website</label>
<input type="url" id="Website" name="Website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="Message">Message:</label>
<textarea rows="6" id="Message" cols="40" name="Message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>

I think you got typo there, change
$headers = "From: $email\r\n";
to
$headers = "From: $emailField\r\n";
Remember to validate the $emailField to avoid injections!
Update: Now that I can see the actual HTML for the form, there is some problems.
I think for requires and ID field, so you should add one to every field in the form.
You also forgot to set name in the name text field.
Lets add the name attribute in the name field, and add the ID field (which should be done for the other fields also):
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
to
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your Name" required />
$_POST['Name'] is case-sensetive, so you should change your code so the variables match the names in the html.
example:
$nameField = $_POST['Name'];
should be
$nameField = $_POST['name'];

put $nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message']; within a post function. After verifying $_post is not empty. on form submit.

Related

Form not returning data

I have a form on my site. When the user submits the form I get the email response but there is no form data returned. All I see is
Company:
Name:
Email:
Phone:
The form html is below:
<form method="post" action="send_survey.php" class="survey" >
<header><img src="../img/PTS_Survey_logo.jpg" alt="Patterson Tubular Services Logo">
Customer Survey</header>
<fieldset>
<section>
<label class="input">
<i class="icon-append icon-group"></i>
<input type="company" id="company" required placeholder="Company name">
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-user"></i>
<input type="name" id="name" required placeholder="Your name" >
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-envelope-alt"></i>
<input type="email" id="email" required placeholder="Your e-mail">
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-phone"></i>
<input type="phone" id="phone" placeholder="Your phone number">
</label>
</section>
</fieldset>
<footer>
<input id="submit" name="submit" type="submit" value="submit" class="button">Submit the survey</button>
</footer>
</form>
The php code is below:
<?php
/*Subject and email variables */
$emailSubject = 'PTS Site Test';
$emailTo = 'testemail#testemail.com';
/* Gather form information */
$company = $_POST['company'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$body = <<<EOD
<br><hr><br>
Company: $company <br>
Name: $name <br>
Email: $email <br>
Phone: $phone <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail ($emailTo, $emailSubject, $body, $headers);
/* Results rendered */
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=http://www.cudd.com/PTS-2014/index.html">';
?>
Thank you in advance.
HTML form input is set in POST by the element's name attribute, not its id. You should set the name attribute to the same as the id on each of your input elements.
You have no name attribute on any of your inputs
Change:
<input type="company" id="company" required placeholder="Company name">
To:
<input type="company" name="company" id="company" required placeholder="Company name">

PHP form e-mail doesn't send

I have written some PHP code for a contact form for my one page portfolio, when it is submitted it just opens up a blank page and also the e-mail isnt sent and the text is not echo'd out. I am not very confident with PHP as it is fairly new to me. if anyone could help that would be great?
html -
<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
<li>
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
</li>
<li>
<label for="email">Email</label>
<input id="email" name="email" type="email" placeholder="example#domain.com" required>
</li>
<li>
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text" placeholder="What the message is about" required>
</li>
<li>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
</textarea>
</li>
<li>
<label for="human">What is 2 + 2 ?</label>
<input id="human" name="human" type="number" placeholder="Please insert answer" required>
</li>
</ol>
</fieldset>
<fieldset>
<input class="button" id="submit" type="submit" value="Send it!">
</fieldset>
</form>
php -
<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: $email';
$to = 'ben_humphries#hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$human = $POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) { //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
} else {
echo "<p>Something went wrong, go back and try again!</p>";
}
} else if ($_POST['submit'] && $human != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>
There were a few things wrong with your form and handler.
In your HTML form, you did not have an input for human so I added that, plus your submit button was not named, so that alone would not have worked.
<input class="button" id="submit" name="submit" type="submit" value="Send it!">
And in your PHP handler, your (mail headers) were improperly formatted, so that ended up in my SPAM box so I added that as well.
$from = $_POST['email'];
as well as headers plus I fixed your last condition to:
if (!isset($_POST['submit']) && ($_POST['human']) != '4')
HTML form
<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
<li>
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
</li>
<li>
<label for="email">Email</label>
<input id="email" name="email" type="email" placeholder="example#domain.com" required>
</li>
<li>
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text" placeholder="What the message is about" required>
</li>
<li>
<label for="human">What is 2 + 2 ?</label>
<input id="human" name="human" type="number" placeholder="Please insert answer" required>
</li>
<li>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
</textarea>
</li>
</ol>
</fieldset>
<fieldset>
<input class="button" id="submit" name="submit" type="submit" value="Send it!">
</fieldset>
</form>
PHP handler, tested and working for you.
<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = $_POST['email'];
$to = 'ben_humphries#hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && ($_POST['human']) == '4') {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
if (!isset($_POST['submit']) && ($_POST['human']) != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>

Add Form Data on PHP mail script

i am using PHP form with File attachment. Due to low knowledge in PHP i use this script from a online tutorial.
My problem is:
I want to get all user submitted form info by mail but don't know how to add code on this script(check last code section).
I want user can only send only .txt, .doc, .docx file format on file attachment.
HTML Code:
<form name="sendmail" action="form.php" method="post" enctype="multipart/form-data">
<ul>
<li>
<span class="left">
<label for="name">First Name:</label>
<input type="text" size="40" id="f_name" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Middle Name:</label>
<input type="text" size="40" id="m_name" />
</span>
<span class="right">
<label for="name">Last Name:</label>
<input type="text" size="40" id="l_name" />
</span>
</li>
<li>
<span class="left">
<label for="email">Email Address:</label>
<input type="email" size="40" id="email" style="width:250px;" />
</span>
<span class="right">
<label for="email" style="width:200px;">Confirm Email Address:</label>
<input type="email" size="40" id="c_email" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Primary Phone: </label>
<input type="text" size="40" id="p_phone" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Secondary Phone:</label>
<input type="text" size="40" id="s_phone" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Mailing Address:</label>
<input type="text" size="40" id="m_address" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Mailing City:</label>
<input type="text" size="40" id="m_city" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">State/Province:</label>
<input type="text" size="30" id="state_province" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Zip/Postal Code:</label>
<input type="text" size="30" id="zip_postal" />
</span>
<span class="right">
<label for="name">Country:</label>
<input type="text" size="30" id="country" />
</span>
</li>
<li>
<span class="left">
<label for="name">Job Information:</label>
<input type="text" size="40" id="job_info" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Desired Job Title: </label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name" style="width:200px;">Desired Geographic Region:</label>
<input type="text" size="40" id="job_info" style="width:200px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Years of Related Experience:</label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label>Resume:</label>
<input type="file" name="resume" />
</span>
<span class="right">
<label style="width:180px;">Cover Letter:</label>
<input type="file" name="cover_letter" />
</span>
</li>
</ul>
<p>
<button type="submit" class="action" name="submit">Submit</button>
</p>
form.php code-
<?php
$from = "info#arif-khan.net";
$to = "arifkpi#gmail.com";
$subject ="JobSeeker Registration Request";
$message = $_POST['body'];
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
// File names of selected files
$filename1 = $_FILES['resume']['name'];
$filename2 = $_FILES['cover_letter']['name'];
// array of filenames to be as attachments
$files = array($file1, $file2);
$filenames = array($filename1, $filename2);
// include the from email in the headers
$headers = "From: $from";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";
// attach the attachments to the message
for($x=0; $x<count($files); $x++){
$file = fopen($files[$x],"r");
$content = fread($file,filesize($files[$x]));
fclose($file);
$content = chunk_split(base64_encode($content));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
$message .= "--{$boundary}\n";
}
// sending mail
$sendmail = mail($to, $subject, $message, $headers);
// verify if mail is sent or not
if ($sendmail) {
header("location:index.html");
}
?>
Field code that i want to add on email body-
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Email Address: $_POST[email]
Primary Phone: $_POST[p_phone]
Secondary Phone: $_POST[s_phone]
Mailing Address: $_POST[m_address]
Mailing City: $_POST[m_city]
State/Province: $_POST[state_province]
Zip/Postal Code: $_POST[zip_postal]
Country: $_POST[country]
Job Information: $_POST[job_info]
Desired Job Title: $_POST[job_title]
Desired Geographic Region: $_POST[desired_region]
Years of Related Experience: $_POST[year_of_experience]
To add the captured data to the sendmail script:
name your variable correctly (that is no space in between), for example,
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Should be (remember the '$' sign to tell php it is a variable:
$First_Name: $_POST['f_name'];
$Middle_Name: $_POST['m_name'];
$Last_Name: $_POST['l_name'];
Do the same for all, and place this code before this script:
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
In your code where you have:
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
$message .= "--{$boundary}\n";
Add this:
$message .= "First name: ".clean_string($First_Name)."\r\n";
$message .= "Last name: ".clean_string($Last_Name)."\r\n";
Do this for all the variable you want to add from (1) above, and your script should now work fine.
Your inputs are missing the name attribute. For PHP to capture form values you need to add the name attribute to the inputs and then capture the input values by referring to these names. For example:
You are capturing the First Name from the from like so:
First Name: $_POST[f_name]
So get the name your input should have a name attribute equal to f_name like so:
<input type="text"name="f_name"size="40" id="job_title" style="width:250px;" />

PHP returns blank fields in email form

i'm getting blank field returns in my inbox on only a portion on my form fields
like so:
From: whatevername
Email: fgsdfg#fakeysite.com
Message:
phone:
here is my code its probably something really dumb but its bugging me, so here we go.
HTML
<div class="row-item col-1_4">
<h3>Contact Form</h3>
<h4>Please fill out the form to get your free CD Replacement Kit</h4>
<!-- Success Message -->
<div class="form-message"></div>
<!-- Form -->
<form class="b-form b-contact-form" action="blast.php">
<div class="input-wrap m-full-width">
<i class="icon-user"></i>
Name
<input class="field-name" type="text" placeholder="Name (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-phone"></i>
Phone
<input class="field-phone" type="text" placeholder="Phone">
</div>
<div class="input-wrap m-full-width">
<i class="icon-envelope"></i>
Email
<input class="field-email" type="text" placeholder="E-mail (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-pencil"></i>
Message
<input class="field-comments" type="text" placeholder="Message">
</div>
<input class="btn-submit btn colored" type="submit" value="Send">
</form>
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient = "csmith#legacybrokerage.com";
$subject = "UNLIMITED ANNUITY LEADS CD BLAST";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
try this, define name= 'something' to the input field
<input class="field-name" type="text" placeholder="Name (required)" name="name">
<input class="field-phone" type="text" placeholder="Phone" name="phone">
<input class="field-email" type="text" placeholder="E-mail (required)" name="email">
your input's dose not name attribute!
<input class="field-name" type="text" placeholder="Name (required)">
correct :
<input name="from" class="field-name" type="text" placeholder="Name (required)">

PHP form submission returns blank

I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.

Categories