gpt4 book ai didi

javascript - jQuery ajax 调用 php 处理表单页面未返回成功或错误

转载 作者:行者123 更新时间:2023-12-03 11:46:25 26 4
gpt4 key购买 nike

我有一个联系页面 HTML Form .

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

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

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

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

IF内部检查 mail() 是否存在的语句方法成功我echo成功或错误消息 json_encode() .

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

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

我删除了 jQuery .click 中的 return false 并尝试了标准 PHP echo在 php 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 代码中......我想。

最佳答案

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

如果您查看上面的 PHP 代码,会发现有一个方法表示 $nextEventDay = getDay();

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

给@MartyMcKeever 的评论投赞成票。

关于javascript - jQuery ajax 调用 php 处理表单页面未返回成功或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com