jQuery对php进程表单页面的ajax调用未返回成功或错误


jQuery ajax call to php process form page not returning success or error

我有一个带有HTML Form的联系人页面。

我使用jQuery来验证字段。然后使用jQuery's .ajax()方法将信息发送到php文件,php文件通过mail()方法处理并发送电子邮件。

PHP File中,我有一个IF语句,它检查是否设置了POST variable,这样我就可以通过网站将多个表单的所有表单处理放在一个页面上。

我获取所有变量,并使用PHP进行另一个表单字段验证。然后我构建了HTML Email及其标头。

如果PHP验证成功,我会通过mail()方法发送电子邮件。然后我检查mail()方法是否成功,如果成功,我会发送另一封"自动回复"电子邮件。

在检查mail()方法是否成功的IF语句内部,我会向echo发送带有json_encode()的成功或错误消息。

当用户点击表单上的提交按钮时,我将其设置为返回false,这样它就会停留在同一页面上,并在发送成功时显示一条消息。

两封电子邮件都从表单成功发送。除了我的.ajax()方法没有收到来自php文件的json_encode()的成功或错误消息。

我删除了jQuery.click中的return false,并在php IF语句中尝试了一个标准的PHP echo,该语句检查post isset()和我是否无法将其打印到浏览器。CCD_ 21也不打印到浏览器。然而,当我把PHP echo放在IF语句之外时,它打印得很好。这让我很困惑,因为它显然进入了if声明发送电子邮件的内部,但不会回复。我做错了什么?

jQuery

$("#contactFormSubmit").click(function(){
      // validate and process form here
        // NAME VALIDATION
        var name = $("input#nameField").val();
        if (name == "") {
            $("input#nameField").focus();
            $("input#nameField").css("border","1px solid red");
            alert("nameFieldError");
            return false;
        }
        // EMAIL VALIDATION
        var formEmail = $("input#emailField").val();
        if (formEmail == "" || !validateEmail(formEmail)) {
            $("input#emailField").focus();
            $("input#emailField").css("border","1px solid red");
            alert("emailFieldError");
            return false;
        }
        // PHONE VALIDATION
        var phone = $("input#phoneField").val();
        var phoneReg = "/'(?([0-9]{3})')?([ .-]?)([0-9]{3})'2([0-9]{4})/";
        if (phone == "" || !isValidUSPhoneFormat(phone)) {
            //alert("phone is wrong");
            $("input#phoneField").focus();
            $("input#phoneField").css("border","1px solid red");
            alert("phoneFieldError");
            return false;   
        }
        var message = $("textarea#messageField").val();
        var subject = $("#subjectField").val();
        var dataString = 'name='+ name + '&email=' + formEmail + '&phone=' + phone + '&subject=' + subject + '&message=' + message + '&submitContactForm=1';
        $.ajax({
            type: "POST",
            url: "process-form.php",
            data: dataString,
            dataType:"json",
            success: function(response) {
                if(response.status === "success") {
                    alert("success");
                    // do something with response.status or other data on success
                } else if(response.status === "error") {
                    alert("error");
                    // do something with response.status or other data on error
                }   
            },
            error: function(xhr,errmsg) { alert(errmsg); }
        });
        return false;      
    });

PHP文件

<?php
include "includes/functions.php";
if(isset($_POST['submitContactForm'])) {
$nextEventDay = getDay();
$eventToShow = "";
$dayToShow = "";
$dateToShow = "";
if ($nextEventDay == "Sunday" || $nextEventDay == "Monday" || $nextEventDay == "Tuesday" || $nextEventDay == "Wednesday" ||$nextEventDay == "Thursday" || $nextEventDay != "Friday" || $nextEventDay != "Saturday") {
    $eventToShow = "thurEvent";
    $dayToShow = "THU";
    $dateToShow = getNextThursdayDate();
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$messageBody = $_POST['message'];
$sendTo = "xxxx@xxxx.com";
$confirmTo = $email;
//HTML EMAIL
        $headers = "From: " . $email . "'r'n";
        $headers .= "MIME-Version: 1.0'r'n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
        //COMPANY EMAIL
        $message5 = '<html><style type="text/css">table, td, tr, th{border:none !important;border-color:#111 !important;border-collapse:collapse;}a{color:#c92626 !important;}.title{color:#aaa;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
        $message5 .= '<table rules="all" style="background-color:#111;border:none !important;width:100%;" cellpadding="10" border="0">';
        $message5 .= '<tr style="border:none;"><td style="border:none;padding:20px 0px !important;"><img src="images/logo.jpg" alt="Nightclub" style="display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;" width="260" /></td></tr>';
        $message5 .= "<tr style='border:none;'><td style='border:none;'><img src='/images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
        $message5 .= "<tr style='border:none;background-color:#161616;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name:</strong> &nbsp;&nbsp;&nbsp;" . strip_tags($name) . "</td></tr>";
        $message5 .= "<tr style='border:none;'><td style='border:none;' class='title' ><strong style='color:#aaa;'>Email:</strong> &nbsp;&nbsp;&nbsp;<span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
        $message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> &nbsp;&nbsp;&nbsp;<span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
        $message5 .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> &nbsp;&nbsp;&nbsp;" . strip_tags($subject) . "</td></tr>";
        $message5 .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> &nbsp;&nbsp;&nbsp;" . strip_tags($messageBody) . "</td></tr>";
        $message5 .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
        $message5 .= "</table>";
        $message5 .= "</body></html>";
        //CLIENT EMAIL
        $areply = '<html><style type="text/css">table, td, tr, th {border:none !important;border-color:#111 !important;border-collapse:collapse;}a {color:#c92626 !important;}.title{color:#aaa;}#date a{color:#fff !important;text-decoration:none;}</style><body style="background-color: #111;color:#ddd;border:2px solid #333;">';
        $areply .= "<table rules='all' style='background-color:#111;border:none !important;width:100%;' cellpadding='10' border='0'>";
        $areply .= "<tr style='border:none;'><td style='border:none;padding:20px 0px !important;'><img src='images/logo.jpg' alt='Nightclub Ann Arbor' style='display:block;margin:0 auto;min-width:260px;max-width:300px;width:50%;' width='260' /></td></tr>";
        $areply .= "<tr style='border:none;'><td style='border:none;'><img src='images/" . $eventToShow . "-email-next-event.jpg' width='260' style='min-width:260px;max-width:1024px;width:100%;display:block;margin: 0 auto;' /></td></tr>";
        $areply .= "<tr style='border:none; background:#151515;'><td style='border:none;text-align:justify;background-color:#161616;'><div style='float:left;display:inline-block;background-color:#000;margin:0px 10px 10px 0px;font-size:197%; padding: 25px 30px;'><p id='date' style='margin:0;color:#fff !important;'> 'r'n <strong>" . $dateToShow ."</strong></p><p style='margin:0;color:#c92626 !important;'>" . $dayToShow ."</p></div><p style='margin-top:10px;margin-right:15px;'>Thank you for contacting us at Nightclub . We look forward to assisting you. Below is the information that we recieved and we will be contacting you as soon as possible. Thank you again, and we look forward to speaking with you. If you have any additional questions please contact us at our website (<a href='' style='color:#c92626'></a>), give us a call <span style='color:#c92626 !important;'></span>, or send us an Email <span style='color:#c92626 !important;'></span></p></td></tr>";
        $areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Name: </strong>" . strip_tags($name) . "</td></tr>";
        $areply .= "<tr style='border:none; background-color:#161616 !important;' ><td style='border:none !important;background-color:#161616 !important;' class='title'><strong style='color:#aaa;'>Email:</strong> &nbsp;&nbsp;&nbsp;<span style='color:#c92626 !important;'>" . strip_tags($email) . "</span></td></tr>";
        $areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Phone:</strong> &nbsp;&nbsp;&nbsp;<span style='color:#c92626 !important;'>" . strip_tags($phone) . "</span></td></tr>";
        $areply .= "<tr style='border:none;background-color:#161616;' ><td style='border:none;' class='title'><strong style='color:#aaa;'>Subject:</strong> &nbsp;&nbsp;&nbsp;" . strip_tags($subject) . "</td></tr>";
        $areply .= "<tr style='border:none;'><td style='border:none;' class='title'><strong style='color:#aaa;'>Message:</strong> &nbsp;&nbsp;&nbsp;" . strip_tags($messageBody) . "</td></tr>";
        $areply .= "<tr style='border:none;'><td style='border:none;'></td></tr>";
        $areply .= "</table>";
        $areply .= "</body></html>";
        $subject2 = "Thank you for your expressed interest ()";
        $noreply = "xxx@xxx.com";   
        $headers2 = "From: " . $noreply . "'r'n";
        $headers2 .= "MIME-Version: 1.0'r'n";
        $headers2 .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
        if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^('d['s-]?)?['('['s-]{0,2}?'d{3}[')']'s-]{0,2}?'d{3}['s-]?'d{4}$/i",$phone)) {
            echo json_encode(array(
                'status' => 'error'
                //'message'=> 'error message'
            ));
        } else {
            $send = mail($sendTo, $subject, $message5, $headers);
        }
        if($send){
            echo json_encode(array(
                'status' => 'success'
                //'message'=> 'success message'
            ));
            $send2 = mail($email, $subject2, $areply, $headers2);
        } else {
            echo json_encode(array(
                'status' => 'error'
                //'message'=> 'success message'
            ));
        }
}
?>

更新我将错误回调添加到jQuery ajax()方法中,发现我收到了错误,我还从PHP文件中删除了除之外的所有内容

            echo json_encode(array(
                'status' => 'success'
                //'message'=> 'success message'
            ));

我仍然从jQuery ajax()方法中得到错误消息。所以它一定在我的jQuery代码中。。。我想。

好吧,多亏了上面@PatrickEvans和@MartyMMcKeever的建议,我通过查看firebug控制台发现我的一个自定义PHP函数缺少一个必需的参数来解决这个问题。这导致了一个解析错误,然后我的jQuery抛出了一个错误,但仍然允许PHP处理电子邮件。

如果你看上面的PHP代码,有一个方法说$nextEventDay = getDay();

应该是$nextEventDay = getDay("today");。这导致了的所有问题

给@MartyMMcKeever的评论投赞成票。