我從發送表單細節的jQuery ajax請求得到一個奇怪的結果給PHP腳本。相同的腳本可以在其他地方免費使用。基本上形式使用jQuery.ajax
這樣提交:在jQuery.ajax成功處理程序中使用來自PHP腳本的返回值的問題
//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
如上完全相同的腳本表示工作正常別的地方,但在這裏我得到這個錯誤並停止腳本。有人可以幫助瞭解可能出現問題的位置嗎?
「正確組織數據」 - 你不是,數據需要轉義。只需構造一個對象並使用它來代替字符串,jQuery就會爲你做到這一點。我會告訴你如何......但「從所有字段獲取數據」意味着你應該只使用['jQuery('form#myForm').serialize()'](http://api.jquery.com/連載/)。 – Quentin 2012-01-03 13:16:14