jQuery ajax调用php进程表单页面没有返回成功或错误

时间:2014-09-24 23:13:42

标签: javascript php jquery ajax

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

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

PHP File内部我有一个IF语句,用于检查是否设置了POST variable这样我可以通过网站将所有表单处理放在一个页面上以显示多个表单

我获取所有变量并使用PHP执行另一个表单字段验证。然后我构建了HTML Email和它的标题。

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

在检查IF方法是否成功的mail()语句内{I} echo成功或错误消息json_encode()

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

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

我在jQuery .click中删除了返回false,并在php PHP echo语句中尝试了一个标准的IF,检查帖子isset()和我是否无法将其发送到打印到浏览器。 json_encode也不会打印到浏览器。但是,当我将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代码中......我想。

1 个答案:

答案 0 :(得分:1)

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

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

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

给@ MartyMcKeever的评论投票。

相关问题