繁体   English   中英

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

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

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

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

PHP File我有一个IF语句,检查是否以这种方式设置了POST variable ,这样我就可以将所有表单处理都放到一个页面上,以便遍历整个网站。

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

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

在用于检查mail()方法是否成功的IF语句内部,我使用json_encode() echo成功或错误消息。

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

这两封电子邮件均通过表格成功发送。 除了我的.ajax()方法未从php文件的json_encode()接收成功或错误消息。

我在jQuery .click中删除了return false,并在php IF语句中尝试了标准的PHP echo ,该语句检查是否发布了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代码中...我认为。

好的,因此,感谢上面的@PatrickEvans和@MartyMcKeever的建议,我通过查看萤火虫控制台来发现我的自定义PHP函数之一缺少必需的参数,从而解决了该问题。 这导致了解析错误,这随后使我的jQuery抛出错误,但仍然允许PHP处理电子邮件。

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

应该是$nextEventDay = getDay("today"); 这是造成所有问题的原因

对@MartyMcKeever的评论进行投票。

暂无
暂无

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

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