One of my first php forms, and I'm having some trouble. Form will not submit. I'm sure this is a combination of problems. I feel like I've used all appropriate id tags and such. Not sure if this is a problem with the redirect, or the mail function, or the validator.
HTML:
<div id="emailform">
<h2>Confirm your purchase information</h2>
<hr>
<form method="post" name="contactform" action="mail_form.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email">
</p>
<p>
<label for='purchasecode'>Purchase Code:</label> <br>
<input type="text" name="purchasecode">
</p>
<p>
<label for='vendor'>Vendor Name:</label> <br>
<select name="vendor">
<option value="1" selected="selected"></option>
<option value="2" >Amazon</option>
<option value="3" >Barnes & Noble</option>
<option value="4" >Family Christian</option>
<option value="5" >Christianbook.com</option>
<option value="6" >LifeWay</option>
<option value="7" >Books-A-Million</option>
<option value="8" >Mardel</option>
</select>
</p>
<button type="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>
</form>
PHP:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];
//Validate first
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['purchasecode']) ||
empty($_POST['vendor']))
{
echo "All fields are required.";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $email;
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
" Here are the details:\n Name: $name \n Email: $email \n Purchase Code \n $purchasecode \n Vendor \n $vendor";.
$to = "name@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
There were 2 errors in your code actually.
1) The dot at the end of:
$email_body = "You have received a new message from the user $name.\n".
" Here are the details:\n Name: $name \n Email: $email \n Purchase Code \n $purchasecode \n Vendor \n $vendor";.
Which needs to be replaced by:
$email_body = "You have received a new message from the user $name.\n".
" Here are the details:\n Name: $name \n Email: $email \n Purchase Code \n $purchasecode \n Vendor \n $vendor";
2) The Submit button was not named, therefore it would always show the error message error; you need to submit the form
error; you need to submit the form
even when the form was successfully sent.
Solution:
<button type="submit" name="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>
HTML form
<div id="emailform">
<h2>Confirm your purchase information</h2>
<hr>
<form method="post" name="contactform" action="mail_form.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email">
</p>
<p>
<label for='purchasecode'>Purchase Code:</label> <br>
<input type="text" name="purchasecode">
</p>
<p>
<label for='vendor'>Vendor Name:</label> <br>
<select name="vendor">
<option value="1" selected="selected"></option>
<option value="2" >Amazon</option>
<option value="3" >Barnes & Noble</option>
<option value="4" >Family Christian</option>
<option value="5" >Christianbook.com</option>
<option value="6" >LifeWay</option>
<option value="7" >Books-A-Million</option>
<option value="8" >Mardel</option>
</select>
</p>
<button type="submit" name="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>
</form>
PHP handler
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];
//Validate first
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['purchasecode']) ||
empty($_POST['vendor']))
{
echo "All fields are required.";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $email;
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
" Here are the details:\n Name: $name \n Email: $email \n Purchase Code \n $purchasecode \n Vendor \n $vendor";
$to = "name@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');
// echo "success";
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.