php邮件表单提交-500错误


php mail form submit - 500 error

我的第一个php表单之一,我遇到了一些麻烦。表格将不会提交。我确信这是多种问题的结合。我觉得我已经使用了所有合适的id标签等等。不确定这是否是重定向、邮件功能或验证器的问题。

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 &amp; 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;
  }
}
?>

实际上您的代码中有2个错误。

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";.

需要替换为:

$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) Submit按钮未命名,因此即使表单已成功发送,它也始终显示错误消息error; you need to submit the form

解决方案:

<button type="submit" name="submit" id="submitbutton" value="Submit" class="mainButton">SUBMIT</button><br>

经过充分测试的代码

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 &amp; 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处理程序

<?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;
  }
}
?>