[英]php mail form submit - 500 error
One of my first php forms, and I'm having some trouble. 我的第一个php形式之一,我遇到了一些麻烦。 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.
我觉得我已经使用了所有适当的id标签等。 Not sure if this is a problem with the redirect, or the mail function, or the validator.
不确定重定向,邮件功能或验证程序是否存在此问题。
HTML: 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:
<?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. 实际上,您的代码中有2个错误。
1) The dot at the end of: 1)点末:
$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
2)未命名Submit按钮,因此它将始终显示错误消息
error; you need to submit the form
error; you need to submit the form
even when the form was successfully sent. 即使表单已成功发送,
error; you need to submit the form
。
Solution: 解:
<button type="submit" name="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>
HTML form 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" name="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>
</form>
PHP handler 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');
// 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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.