0

我的第一个 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;
  }
}

?>
4

1 回答 1

4

实际上,您的代码中有 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)提交按钮没有命名,因此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;
  }
}

?>
于 2013-09-04T12:18:26.437 回答