我从向jQuery脚本发送表单详细信息的PHP请求中得到一个奇怪的结果。同样的脚本在其他地方使用也没有问题。基本上,表单是使用jQuery.ajax
提交的,如下所示:
//if submit button is clicked
$('#form1').submit(function () {
//Get the data from all the fields
var name = $('input[name=name]');
var email = $('input[name=email]');
var con_email = $('input[name=con_email]');
var comments = $('textarea[name=comments]');
//organize the data properly
var data = 'name=' + name.val() + '&email=' + email.val() + '&con_email=' + con_email.val() + '&comments=' + encodeURIComponent(comments.val());
//show the loading sign
$('.loading').show();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "process-email/process.php",
//GET method is used
type: "GET",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function () {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.form').fadeOut('slow');
$('.done').delay(1000).fadeIn('slow');
}
}
});
//cancel the submit button default behaviours
return false;
});
PHP脚本运行良好,发送电子邮件并返回1
(已发送电子邮件),但脚本停止于:if(html==1)
。我得到了这个错误
html is not defined
如上所述,完全相同的脚本在其他地方运行得很好,但在这里我得到了那个错误,脚本被停止了。有没有人能帮我了解一下哪里可能出了问题?
发布于 2012-01-03 13:08:11
您必须添加参数引用:
success: function (html) {
//if process.php returned 1/true (send mail success)
//.....
}
然后,您可以使用此参数,它将是来自服务器的响应。
发布于 2012-01-03 13:09:18
看起来您没有将响应从PHP脚本返回到JavaScript函数。如果你对你的成功函数做了类似以下的事情,它应该会让你走上正确的道路:
success: function( html )
{
if(html=='1')
{
[...]
}
}
https://stackoverflow.com/questions/8712730
复制