【问题标题】:PHP Email Form Not Submitting [closed]PHP电子邮件表单未提交[关闭]
【发布时间】:2014-10-08 17:23:55
【问题描述】:

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

因此,我使用链接到本地​​主机上的“html_form_send.php”的“/contacts.html”页面设置了一个联系人表单。一旦用户按下“提交”,此消息就会出现在下一页:

“非常抱歉,您提交的表单存在错误。这些错误显示在下方。

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

请返回并修复这些错误。”

无论您是否正确或错误地填写了所有字段或未填写任何字段,都会显示此消息。为什么?

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

任何帮助将不胜感激,非常感谢!

【问题讨论】:

  • 现在查看下面的raidenace' 答案并仔细阅读。它将解决问题。如果它仍然不起作用,那么您需要开始检查您的错误。确保您有mail() 可用,并安装、运行和正确配置了 PHP。感谢acceptinganswer的raidenace。

标签: php html forms email contact


【解决方案1】:

变化:

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

到:

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

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

正如@Fred-ii 指出的那样,您正在POST 中测试telephone,但html 中的值是phone

AND 删除 - 创建第二个表单的 &lt;form action="index.php"&gt;

顺便说一句,在 SO 中发布此问题只需花费一半的精力,即可运行一些快速调试 echo/print_r/error_reporting 语句并弄清楚这一点。如果您打算在未来进行更多的编程,我强烈建议您提高调试技能,对于这样的小问题更容易和更有效。对于更大的,总是有这样的:-)

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

【讨论】:

  • 其实你错过了一个。
  • @Fred-ii- 错过了什么?
  • Yepper +1 另外,OP 有两个 &lt;form 标签,只有一个 &lt;/form&gt; 可能会造成问题。 OP 的代码有太多问题。
  • @Fred-ii-sigh 这没有尽头吗? :( 好吧,我已经完成了指出,(其中大部分是你的 btw :-),我想 OP 应该开始帮助自己,就像我在回答中指出的那样......
  • @Fred-ii-so true,这就是为什么我一直认为在关闭错误报告的情况下在开发环境中编程是一种犯罪行为......我实际上遇到过甚至不知道的程序员如何打开它!我的意思是像那些以此为生的人,而不是建立家庭网站的业余爱好者......
猜你喜欢
  • 2018-11-05
  • 2013-09-16
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多