简体   繁体   English

AJAX联系表单未发布时的错误消息

[英]Error messages when AJAX contact form doesn't post

I have several contact forms using the same general AJAX code: 我有几个使用相同的通用AJAX代码的联系表单:

$(document).ready(function() {
    $('#mainMail').submit( function() {
        $.ajax({
            type: "POST",

            url : "<?php echo home_url(); ?>/wp-content/themes/retlehs-roots-f45f4f5/assets/bin/process.php",
            data: $(this).serialize(),
            success: function() {
                    $('#enquiryBox #submitSuccess').removeClass('hidden');
                    setTimeout(function() { $("#enquiryBox #submitSuccess").addClass('hidden'); }, 15000);       
                },
            error: function() {
                $('#enquiryBox #submitFail').removeClass('hidden');
                setTimeout(function() { $("#enquiryBox #submitFail").addClass('hidden'); }, 15000);
            }
            });
        return false;
    });
});

In short, when it successfully submits the form and emails me (via process.php) the #submitSuccess div DOES show up as it should. 简而言之,当它成功提交表单并通过电子邮件发送给我(通过process.php)时, #submitSuccess div就会显示出来。 Problem is, when it fails, the div also shows up (the #submitFail div stays hidden). 问题是,当它失败时,div 也会出现( #submitFail div保持隐藏状态)。

I'm guessing that this is because the ajax post failure would only occur if the process.php isn't successfully called? 我猜这是因为只有在没有成功调用process.php时才会发生ajax post失败? Is there any way to adjust this so that if the email isn't sent to me via process.php, the user gets an error message? 有没有办法调整这个,以便如果没有通过process.php发送给我的电子邮件,用户会收到一条错误消息?

Here's my process.php code: 这是我的process.php代码:

<?php 

$robotest = $_POST['robotest']; //just testin' for robots

$recipient = "info@mydomain.com"; //recipient 
$email = ($_POST['email']); //senders e-mail adress 

if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) { 

$Name = ($_POST['name']); //senders name 

$mail_body  = "Hello, \r\n \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n\r\n";
$mail_body .= "{$_POST['comments']}; \r\n \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: {$_POST['phone']} \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";



$subject = "Free Consult Inquiry"; //subject 
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 

} else {
  print "You've entered an invalid email address!";
 }
?>

(the print "you've entered an invalid email address!" was my attempt at an error message, but that's not working either...) print "you've entered an invalid email address!"是我尝试收到错误消息,但这不起作用......)

Add this line $("#enquiryBox #submitSuccess").addClass('hidden'); 添加此行$("#enquiryBox #submitSuccess").addClass('hidden'); into the error block as well, 进入错误块,

  $.ajax({
        type: "POST",

        url : "<?php echo home_url(); ?>/wp-content/themes/retlehs-roots-f45f4f5/assets/bin/process.php",
        data: $(this).serialize(),
        success: function() {
                $('#enquiryBox #submitSuccess').removeClass('hidden');
                setTimeout(function() { $("#enquiryBox #submitSuccess").addClass('hidden'); }, 15000);       
            },
        error: function() {
            $('#enquiryBox #submitFail').removeClass('hidden'); //why do you remove class 'hidden' and add it later too ??
            $("#enquiryBox #submitSuccess").addClass('hidden'); //added line here
            setTimeout(function() { $("#enquiryBox #submitFail").addClass('hidden'); }, 15000);
        }
        });

You can indicate an error by returning an HTTP error code, eg 您可以通过返回HTTP错误代码来指示错误,例如

} else {
  header("HTTP/1.1 500 Internal error");
  print "You've entered an invalid email address!";
 }

If you read the manual entry on jQuery.ajax() , you'll find that the error event occurs when a protocol or network error occurs. 如果您阅读jQuery.ajax()手册条目 ,您会发现在发生协议或网络错误时发生错误事件。 If your script return false , null or anything like that, it is still considered a valid response, hence your problem. 如果你的脚本返回falsenull或类似的东西,它仍然被认为是一个有效的响应,因此你的问题。

In such a case, I would write it so that all calls end up calling one method, and if error event is called, it would call that same method with a rebuilt error message. 在这种情况下,我会编写它,以便所有调用最终调用一个方法,如果调用错误事件,它将使用重建的错误消息调用相同的方法。

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

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