PHP电子邮件表单未提交

时间:2014-10-08 17:23:55

标签: php html forms email contact

好的,所以我已经四处寻找这个具体的问题,但是我找不到它,所以我希望这不是重复。

所以我使用我的" /contacts.html"设置了一个联系表单;页面链接到" html_form_send.php"位于我当地的主人。一旦用户按下"提交"此消息出现在下一页:

"我们非常抱歉,但您提交的表单中发现了错误。这些错误显示在下面。

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

请返回并修正这些错误。"

无论是填写所有字段还是填写任何字段,无论是正确还是错误,都会显示此消息。为什么呢?

    <?php
if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "xxxxxxxx.yyyyyyy@gmail.com";

    $email_subject = "Quantum1Connect Contact Request";


    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']; // not 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 -->

Thanks for contacting me! I will respond within 24 hours of receiving your message!

<?php
}
die();
?>

     <div id="content">
        <div class="content_item">
          <h2>Contact Me!</h2>
          <p>Whether you have constructive criticism, advise, questions, or a request, this is the place to do it! </p>
          <form name="htmlform" method="post" action="http://localhost/html_form_send.php">
            <div style="width:170px; float:left;"><p><label for="first_name">First Name *</label></p></div>
              <div style="width:430px; float:right;"><p><input class="contact" type="text" name="first_name" value="" /></p></div>
            <div style="width:170px; float:left;"><p><label for="last_name">Last Name </label></p></div>
                <div style="width:430px; float:right;"><p><input class="contact" type="text" name="last_name" value="" /></p></div>
            <div style="width:170px; float:left;"><p><label for="email">Email Address * </label></p></div>
            <div style="width:430px; float:right;"><p><input class="contact" type="text" name="email" value="" /></p></div>
            <div style="width:170px; float:left;"><p><label for="telephone">Phone Number  </label></p></div>
            <div style="width:430px; float:right;"><p><input class="contact" type="text" name="phone" value="" /></p></div>
            <div style="width:170px; float:left;"><p><label for="comments">Message *</label></p></div>
              <div style="width:430px; float:right;"><p><textarea class="contact textarea" rows="8" cols="50" name="message"></textarea></p></div>
            <br style="clear:both;" />
            <p style="padding: 10px 0 10px 0;">Please enter the answer to this simple math question (to prevent spam)</p>
            <div style="width:170px; float:left;"><p>Maths Question: 9 + 3 = ?</p></div>
              <div style="width:430px; float:right;">
            <p><input type="text" name="user_answer" class="contact" /><input type="hidden" name="answer" value="4d76fe9775"/></p>
            </div>
            <div style="width:430px; float:right;">
            <p style="padding-top: 15px"><form action="index.php"><input type="submit" value="Submit"></p>
            </div>
          </form>
        </div><!--close content_item-->

非常感谢任何帮助,非常感谢你!

1 个答案:

答案 0 :(得分:4)

变化:

<textarea class="contact textarea" rows="8" cols="50" name="message"></textarea>

为:

<textarea class="contact textarea" rows="8" cols="50" name="comments"></textarea>

您已将html textarea的名称设置为消息,但您正在测试php代码中名为 comments 的textarea。

同样正如@ Fred-ii指出的那样,你正在测试POST中的电话,但html中的值是手机

AND 删除 - 创建SECOND表单的<form action="index.php">

顺便说一下,在SO中发布这个问题只需花费一半的时间来运行一些快速调试echo / print_r / error_reporting语句并弄清楚这一点。如果您计划将来进行更多编程,我强烈建议您使用您的调试技巧,这对于这些小问题更容易和更有效。对于较大的,总有SO: - )

不是愤世嫉俗,只是一个建议:)祝你好运!