Html表单未从PHP处理


Html form not being processed from PHP

我有这个html表单

<form action="mail.php" method="post">
    <div class="col-md-9">
      <div class="col-md-4">
        <input type="text" name="name" id="name" class="name" placeholder="Your Name">
      </div>
      <div class="col-md-4">
        <input type="text" name="email" id="email" class="email" placeholder="Your Email">
      </div>
      <div class="col-md-4">
        <input type="text" name="subject" id="subject" class="subject" placeholder="Subject">
      </div>
      <div class="col-md-12">
        <textarea name="message" cols="1" rows="1" class="message" placeholder="Your message... " id="message"></textarea>
      </div>
      <div class="col-md-4">
        <input type="submit" name="send" value="Send Message" id="submit" class="button templatemo_sendbtn">
      </div>
    </div>
  </form>

这是我在网上找到的php,但我无法让它工作。请帮帮我。我正试图将php添加到文件中,但这个编辑器不允许我这样做。似乎发生的事情是,一旦在网页上填写了表单,我就出现了一个错误,需要返回并修复代码,但我不明白是什么错误,因为我对php一无所知。

    <?php
if(isset($_POST['email'])) {
     
    // CHANGE THE TWO LINES BELOW
    $email_to = "name@domain.com";
     
    $email_subject = "website html form submissions";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.'n'n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."'n";
    $email_message .= "Last Name: ".clean_string($last_name)."'n";
    $email_message .= "Email: ".clean_string($email_from)."'n";
    $email_message .= "Telephone: ".clean_string($telephone)."'n";
    $email_message .= "Comments: ".clean_string($comments)."'n";
     
     
// create email headers
$headers = 'From: '.$email_from."'r'n".
'Reply-To: '.$email_from."'r'n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- place your own success html below -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
die();
?>

单击提交/发送后,我收到的错误消息是:很抱歉,您提交的表格有错误。这些错误显示在下面。

很抱歉,您提交的表格似乎有问题。

请返回并修复这些错误。

在您的PHP代码中,您正在到达块,该块正在调用died()函数,该函数正在提取脚本并生成错误消息:

// validation expected data exists
if (!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

这是因为上面的脚本正在查看是否有几个表单字段已发送/发布,而这些字段尚未发送/发布。您没有包含name="first_name"name="last_name"name="telephone"name="comments"的表单input。因此,由于这个条件不满足,它抛出了died()函数。

您可以通过更改HTML表单、删除name="name"、为*first_name and last_name , changing name="message"to name="comments"`添加input以及为电话添加输入来解决此问题:

<form action="mail.php" method="post">
    <div class="col-md-9">
      <div class="col-md-4">
        <input type="text" name="first_name" id="first_name" class="name" placeholder="Your First Name">
      </div>
      <div class="col-md-4">
        <input type="text" name="last_name" id="last_name" class="name" placeholder="Your Last Name">
      </div>
      <div class="col-md-4">
        <input type="text" name="telephone" id="telephone" class="email" placeholder="Your Telephone">
      </div>
      <div class="col-md-4">
        <input type="text" name="email" id="email" class="email" placeholder="Your Email">
      </div>
      <div class="col-md-4">
        <input type="text" name="subject" id="subject" class="subject" placeholder="Subject">
      </div>
      <div class="col-md-12">
        <textarea name="comments" cols="1" rows="1" class="message" placeholder="Your message... " id="comments"></textarea>
      </div>
      <div class="col-md-4">
        <input type="submit" name="send" value="Send Message" id="submit" class="button templatemo_sendbtn">
      </div>
    </div>
</form>

编辑

还必须满足以下条件:

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
    died($error_message);
}

这意味着您必须输入一个有效的电子邮件地址(例如。example@example.com),一个名字(例如John)、一个姓氏(例如Doe)和2个或多个字符输入到表单中的comments文本区域框中。