错误:表单提交失败,并且没有从以下位置返回错误消息:联系人.php

Error: Form submission failed and no error message returned from: contact.php

提问人:Tabish Gill 提问时间:9/25/2023 最后编辑:brombeerTabish Gill 更新时间:9/25/2023 访问量:95

问:

我有这个问题

我的 php 代码是

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

if(isset($_POST['submit']))
{
    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];


//Load Composer's autoloader
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      
    $mail->isSMTP();                                            
    $mail->Host       = 'smtp.gmail.com';                     
    $mail->SMTPAuth   = true;                                   
    $mail->Username   = '[email protected]';                     
    $mail->Password   = '000000000000';                               
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            
    $mail->Port       = 587;                                    


    $mail->setFrom('[email protected]', 'Contactform');
    $mail->addAddress('[email protected]', 'website mail');  


    $mail->isHTML(true);                                  
    $mail->Subject = "Sender Subject - $subject";
    $mail->Body    = "Sender Name - $name <br> Sender Email - $email <br> Sender Subject - $subject <br> Message - $message";

    $mail->send();
    echo "Your message has been sent. Thank you!";
} catch (Exception $e) {
    echo "Message could not be sent.";
}

}

?>

我的 HTML 联系表单代码是

<div class="col-lg-8">
        <form action="contact.php" method="post" role="form" class="php-email-form">
          <div class="row">
            <div class="col-md-6 form-group">
              <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" required>
            </div>
            <div class="col-md-6 form-group mt-3 mt-md-0">
              <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" required>
            </div>
          </div>
          <div class="form-group mt-3">
            <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" required>
          </div>
          <div class="form-group mt-3">
            <textarea class="form-control" name="message" placeholder="Message" required></textarea>
          </div>
          <div class="my-3">
            <div class="loading">Loading</div>
            <div class="error-message">Message could not be sent.</div>
            <div class="sent-message">Your message has been sent. Thank you!</div>
          </div>
          <div class="text-center"><button type="submit" name="submit" value="submit">Send Message</button></div>
        </form>
      </div>

如果我从联系人中删除此代码,PHPMailer 和 contact.php 文件都可以正常工作.php

enter image description here

它连接了我的 html 联系表单和 php 文件以发送电子邮件,它可以正常工作,我的意思是它只将示例电子邮件发送到电子邮件 ID,但它正在工作。

当添加此代码将我的 HTML 联系人表单链接到 Contact.php 文件时,它给了我这个错误

错误:表单提交失败,并且没有从以下位置返回错误消息:联系人.php

PHP HTML 联系人 联系表单

评论

0赞 ADyson 9/25/2023
服务器上的 php 日志中有任何错误吗?
1赞 Rob Eyre 9/25/2023
您是否使用 JavaScript 代码提交表单?如果是这样,请您在这里分享相关部分
0赞 CBroe 9/25/2023
“错误:表单提交失败,没有从以下位置返回错误消息:contact.php” - 这听起来不像任何默认错误消息,但它似乎也不是由您迄今为止向我们展示的任何代码创建的。而且它说“没有返回错误消息”,这听起来好像有客户端JavaScript参与处理这一切。

答:

0赞 Tabish Gill 9/25/2023 #1
This is java script

/** * PHP 电子邮件表单验证 - v3.6 * 网址: https://bootstrapmade.com/php-email-form/ * 作者: BootstrapMade.com */ (函数 () { “使用严格”;

  let forms = document.querySelectorAll('.php-email-form');

  forms.forEach(function (e) {
    e.addEventListener('submit', function (event) {
      event.preventDefault();

      let thisForm = this;

      let action = thisForm.getAttribute('action');
      let recaptcha = thisForm.getAttribute('data-recaptcha-site-key');

      if (!action) {
        displayError(thisForm, 'The form action property is not set!');
        return;
      }
      thisForm.querySelector('.loading').classList.add('d-block');
      thisForm.querySelector('.error-message').classList.remove('d-block');
      thisForm.querySelector('.sent-message').classList.remove('d-block');

      let formData = new FormData(thisForm);

      if (recaptcha) {
        if (typeof grecaptcha !== "undefined") {
          grecaptcha.ready(function () {
            try {
              grecaptcha.execute(recaptcha, { action: 'php_email_form_submit' })
                .then(token => {
                  formData.set('recaptcha-response', token);
                  php_email_form_submit(thisForm, action, formData);
                })
            } catch (error) {
              displayError(thisForm, error);
            }
          });
        } else {
          displayError(thisForm, 'The reCaptcha javascript API url is not loaded!')
        }
      } else {
        php_email_form_submit(thisForm, action, formData);
      }
    });
  });

  function php_email_form_submit(thisForm, action, formData) {
    fetch(action, {
      method: 'POST',
      body: formData,
      headers: { 'X-Requested-With': 'XMLHttpRequest' }
    })
      .then(response => {
        if (response.ok) {
          return response.text();
        } else {
          throw new Error(`${response.status} ${response.statusText} ${response.url}`);
        }
      })
      .then(data => {
        thisForm.querySelector('.loading').classList.remove('d-block');
        if (data.trim() == 'OK') {
          thisForm.querySelector('.sent-message').classList.add('d-block');
          thisForm.reset();
        } else {
          throw new Error(data ? data : 'Form submission failed and no error message returned from: ' + action);
        }
      })
      .catch((error) => {
        displayError(thisForm, error);
      });
  }

  function displayError(thisForm, error) {
    thisForm.querySelector('.loading').classList.remove('d-block');
    thisForm.querySelector('.error-message').innerHTML = error;
    thisForm.querySelector('.error-message').classList.add('d-block');
  }

})();

评论

0赞 Community 9/27/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。