繁体   English   中英

联系表单上的“无法发布/send_form_email.php错误”

[英]“Cannot POST /send_form_email.php error” on contact form

我收到“无法发布/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

}

您的PHP代码永远不会执行,$ _ POST ['email']不存在。 实际上,在您的表单中,您绝对没有'name ='属性-如果您想从$ _POST数组的表单中获取任何内容,这些属性必不可少。

例如,对于像电子邮件这样的输入元素,您可以指定name ='email':

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

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

请注意,type ='email'会自动(如果浏览器支持该类型,否则将还原为文本)检查电子邮件地址是否为有效的“模式”。 如果您不仅需要限制电子邮件地址,而不仅仅是看起来像电子邮件地址的字符串,则可以使用pattern属性-允许您指定正则表达式,该值必须匹配,该值才有效。

因此,建议您首先检查是否使用if(isset($ _ POST ['submit'])))提交表单{您的其余代码可在其中获取/处理$ _POST数组中的值}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM