"无法POST /send_form_email.php错误"在联系表格上

时间:2017-10-07 15:30:17

标签: php html

我得到一个"不能发布/send_form_email.php"错误,还没弄明白为什么?这些文件位于同一目录中。这些是网站上唯一的两个HTML或PHP文件。

  <form id="contactform" action="send_form_email.php" method="post">
            <div class="form-group">
              <label for="firstname">First Name</label>
              <input type="text" class="form-control placeholder="First Name" required>
            </div>

            <div class="form-group">
              <label for="lastname">Last Name</label>
              <input type="text" class="form-control" placeholder="Last Name" required>
            </div>

            <div class="form-group">
              <label for="email">Email</label>
              <input type="text" class="form-control" placeholder="Email" required>
            </div>

            <div class="form-group">
              <label for="subject">Phone</label>
              <input type="text" class="form-control" placeholder="Phone" required>
            </div>

            <div class="form-group">
              <label for="message">Message</label>
              <textarea id="message" rows="3" class="form-control"></textarea>
            </div>

        </div>

        <div class="modal-footer">
          <button class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button name="submit" type="submit" class="btn btn-primary" id="contact-submit">Submit</button>
        </div>
        </form>

这是它链接到的PHP文件。我仔细检查了文件名。我已经清除缓存以确保更改。

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

    // Sent where the email goes here
    $email_to = "jeff.a.winkler@gmail.com";
    $email_subject = "Message from Patient";
        function died($error) {
        // 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['firstname']) ||
        !isset($_POST['lastname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }



    $firstname = $_POST['firstname']; // required
    $lastname = $_POST['lastname']; // required
    $email = $_POST['email']; // required
    $phone = $_POST['telephone']; // not required
    $message = $_POST['message']; // 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($firstname)."\n";
    $email_message .= "Last Name: ".clean_string($lastname)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Telephone: ".clean_string($phone)."\n";
    $email_message .= "Comments: ".clean_string($message)."\n";

// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}

2 个答案:

答案 0 :(得分:0)

您的PHP代码永远不会被执行,$ _POST [&#39; email&#39;]不存在。事实上,在你的表格中,你绝对没有名字=&#39;属性 - 如果你想从$ _POST数组中的表单中选取任何东西,这些是必不可少的。

E.g。对于像电子邮件这样的输入元素,您可以指定姓名=&#39;电子邮件&#39; :

<input type="email" name='email' class="form-control" placeholder="Email" required>

$_POST['email'] will now (when submitted...) hold the e-mail address.

请注意type =&#39;电子邮件&#39;它将自动(如果浏览器支持该类型,否则它将恢复为文本)检查电子邮件地址是否有效&#39;模式&#39;。如果您需要限制的电子邮件地址不仅仅是一个看起来像电子邮件地址的字符串,您可以使用模式属性 - 允许您指定值必须匹配的正则表达式,以使该值有效。

所以我建议您首先检查表单是否已提交if(isset($ _ POST [&#39; submit&#39;])){其余代码,您可以在其中获取/处理值$ _POST array}

答案 1 :(得分:0)

 <form id="contactform" action="send_form_email.php" method="POST">
            <div class="form-group">
              <label for="firstname">First Name</label>
              <input type="text" name="firstname" class="form-control placeholder="First Name" required>
            </div>

            <div class="form-group">
              <label for="lastname">Last Name</label>
              <input type="text" name="lastname" class="form-control" placeholder="Last Name" required>
            </div>

            <div class="form-group">
              <label for="email">Email</label>
              <input type="text" name="email" class="form-control" placeholder="Email" required>
            </div>

            <div class="form-group">
              <label for="subject">Ph`enter code here`one</label>
              <input type="text" name="telephone" class="form-control" placeholder="Phone" required>
            </div>

            <div class="form-group">
              <label for="message">Message</label>
              <textarea id="message" name="message" rows="3" class="form-control"></textarea>
            </div>

        </div>

        <div class="modal-footer">
          <button class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button name="submit" type="submit" class="btn btn-primary" id="contact-submit">Submit</button>
        </div>
        </form>