如何修复jQuery/PHP表单提交?
我正在更新一个客户网站,以前工作的简单联系方式现在已经坏了。当我返回错误消息时,HTML表单似乎从jQuery文件发送和接收数据,尽管它没有将数据传递到PHP文件以发送电子邮件。如果我将数据直接从HTML表单发送到PHP文件,则会发送一封电子邮件。这个错误可能在jQuery的末尾,尽管我对如何修复的搜索还没有给出答案。你有什么想法可以让它工作吗 HTML表单如何修复jQuery/PHP表单提交?,php,jquery,forms,Php,Jquery,Forms,我正在更新一个客户网站,以前工作的简单联系方式现在已经坏了。当我返回错误消息时,HTML表单似乎从jQuery文件发送和接收数据,尽管它没有将数据传递到PHP文件以发送电子邮件。如果我将数据直接从HTML表单发送到PHP文件,则会发送一封电子邮件。这个错误可能在jQuery的末尾,尽管我对如何修复的搜索还没有给出答案。你有什么想法可以让它工作吗 HTML表单 <form id="rsForm" action="#" onsubmit="return goContact(this);" na
<form id="rsForm" action="#" onsubmit="return goContact(this);" name="rsForm">
<input id="formNam" type="hidden" name="formNam" value="3" />
<div class="CU_row">
<div class="CU_form_title"><label for="firstName">First Name:</label></div>
<div class="CU_form_entry"><input id="firstName" maxlength="120" size="39" name="first" type="text" /> <span class="redT">*</span></div>
</div>
<div class="CU_row">
<div class="CU_form_title"><label for="lastName">Last Name:</label></div>
<div class="CU_form_entry"><input id="lastName" maxlength="120" size="39" name="last" type="text" /> <span class="redT">*</span></div>
</div>
<div class="CU_row">
<div class="CU_form_title"><label for="emailAddress">Email:</label></div>
<div class="CU_form_entry"><input id="emailAddress" maxlength="120" size="39" name="email" type="text" /> <span class="redT">*</span></div>
</div>
<div class="CU_row">
<div class="CU_form_title"><label for="subjectLine">Subject:</label></div>
<div class="CU_form_entry"><input id="subjectLine" maxlength="120" size="39" name="subject" type="text" /> <span class="redT">*</span></div>
</div>
<div class="CU_row">
<div class="CU_form_title"><label for="messageCopy">Message:</label></div>
<div class="CU_form_entry"><textarea id="messageCopy" rows="6" cols="30" name="message"></textarea> <span class="redT">*</span></div>
</div>
<div id="CU_reset"><input type="reset" value="Reset" /></div>
<div id="CU_submit"><input type="submit" name="Submit" value="Submit" /></div>
名字:
*
姓氏:
*
电邮:
*
主题:
*
信息:
*
jQuery文件
// JavaScript Document
function goContact(theForm){
//Validate the forms and create the array to send.
var frmName = theForm.formNam.value;
//Validate Common elements.
if(theForm.firstName.value.length < 1){
alert("You must supply a First Name");
theForm.firstName.focus();
return false;
}
if(theForm.lastName.value.length < 1){
alert("You must supply a Last Name");
theForm.lastName.focus();
return false;
}
if(theForm.email.value.length < 1){
alert("You must supply an Email");
theForm.email.focus();
return false;
}
if(theForm.subjectLine.value.length < 1){
alert("You must supply a Subject");
theForm.subjectLine.focus();
return false;
}
if(theForm.messageCopy.value.length < 1){
alert("You must supply a Message");
theForm.messageCopy.focus();
return false;
}
sendAjaxReq($(theForm).serialize(true));
return false;
}
function showResult(messageText){
//Show the pop up with the confirmation.
$('msgWindow').innerHTML = messageText;
$('rsForm').reset();
$('frmInter').hide();
}
function sendAjaxReq(formEms){
//Send he ajax request
var rSp = new Ajax.Request("includes/sendContact.php", {
method: 'get',
parameters: formEms,
onComplete: receiveRespon});
}
function receiveRespon(oReq, JSONRsp){
//Receive the response from the ajax request.\
var result = JSONRsp;
if(result){
showResult(result);
}
}
<?php
if(isset($_GET['Submit'])){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: no-reply@sitename.com' . "\r\n";
'X-Mailer: PHP/' . phpversion();
$to = "info@sitename.com";
$subject = "Inquiry from " . $_SERVER['HTTP_HOST'];
$message = "A client has sent a contact us email\n\n";
foreach($_GET AS $field => $value) {
$message .= "field = $field, value = $value \n\n";
}
$mailSent = mail($to, $subject, $message, $headers);
$arr = "Your message has been received.";
header('X-JSON: ('.json_encode($arr).')');
}
?>
//JavaScript文档
功能goContact(表格){
//验证表单并创建要发送的数组。
var frmName=form.formNam.value;
//验证公共元素。
if(form.firstName.value.length<1){
警告(“您必须提供一个名字”);
form.firstName.focus();
返回false;
}
if(form.lastName.value.length<1){
警告(“您必须提供姓氏”);
form.lastName.focus();
返回false;
}
if(form.email.value.length<1){
提醒(“您必须提供电子邮件”);
form.email.focus();
返回false;
}
if(form.subjectLine.value.length<1){
警告(“您必须提供一个主题”);
form.subjectLine.focus();
返回false;
}
if(form.messageCopy.value.length<1){
警报(“您必须提供消息”);
form.messageCopy.focus();
返回false;
}
sendAjaxReq($(格式).serialize(true));
返回false;
}
函数showResult(messageText){
//显示带有确认信息的弹出窗口。
$('msgWindow').innerHTML=messageText;
$('rsForm').reset();
$('frmInter').hide();
}
函数sendAjaxReq(formEms){
//发送请求
var rSp=new Ajax.Request(“includes/sendContact.php”{
方法:“get”,
参数:formEms,
未完成:接收方});
}
职能接收人(oReq、JSONRsp){
//接收来自ajax请求的响应\
var结果=JSONRsp;
如果(结果){
showResult(result);
}
}
PHP文件
// JavaScript Document
function goContact(theForm){
//Validate the forms and create the array to send.
var frmName = theForm.formNam.value;
//Validate Common elements.
if(theForm.firstName.value.length < 1){
alert("You must supply a First Name");
theForm.firstName.focus();
return false;
}
if(theForm.lastName.value.length < 1){
alert("You must supply a Last Name");
theForm.lastName.focus();
return false;
}
if(theForm.email.value.length < 1){
alert("You must supply an Email");
theForm.email.focus();
return false;
}
if(theForm.subjectLine.value.length < 1){
alert("You must supply a Subject");
theForm.subjectLine.focus();
return false;
}
if(theForm.messageCopy.value.length < 1){
alert("You must supply a Message");
theForm.messageCopy.focus();
return false;
}
sendAjaxReq($(theForm).serialize(true));
return false;
}
function showResult(messageText){
//Show the pop up with the confirmation.
$('msgWindow').innerHTML = messageText;
$('rsForm').reset();
$('frmInter').hide();
}
function sendAjaxReq(formEms){
//Send he ajax request
var rSp = new Ajax.Request("includes/sendContact.php", {
method: 'get',
parameters: formEms,
onComplete: receiveRespon});
}
function receiveRespon(oReq, JSONRsp){
//Receive the response from the ajax request.\
var result = JSONRsp;
if(result){
showResult(result);
}
}
<?php
if(isset($_GET['Submit'])){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: no-reply@sitename.com' . "\r\n";
'X-Mailer: PHP/' . phpversion();
$to = "info@sitename.com";
$subject = "Inquiry from " . $_SERVER['HTTP_HOST'];
$message = "A client has sent a contact us email\n\n";
foreach($_GET AS $field => $value) {
$message .= "field = $field, value = $value \n\n";
}
$mailSent = mail($to, $subject, $message, $headers);
$arr = "Your message has been received.";
header('X-JSON: ('.json_encode($arr).')');
}
?>
您更改了什么?您是否更新了jQuery的版本?东西不会无缘无故地停止工作。$(theForm).serialize(true)和新的Ajax.Request()看起来不像原型jQuery@relentless代码在旧版本的网站上运行,现在经过多次更新后,我注意到它没有运行。谢谢,Jason。我会研究的。这个网站是在版本控制下吗?听起来像是git bisect的工作