2012-01-03 25 views
2

我從發送表單細節的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 

如上完全相同的腳本表示工作正常別的地方,但在這裏我得到這個錯誤並停止腳本。有人可以幫助瞭解可能出現問題的位置嗎?

+0

「正確組織數據」 - 你不是,數據需要轉義。只需構造一個對象並使用它來代替字符串,jQuery就會爲你做到這一點。我會告訴你如何......但「從所有字段獲取數據」意味着你應該只使用['jQuery('form#myForm').serialize()'](http://api.jquery.com/連載/)。 – Quentin 2012-01-03 13:16:14

回答

1

您必須添加參數參考:

success: function (html) { 
    //if process.php returned 1/true (send mail success) 
    //..... 
} 

然後你可以使用這個參數,這將是從服務器的響應。

+0

它已經有 – firefiter 2012-01-03 13:09:24

+0

對不起你了。我現在會嘗試 – firefiter 2012-01-03 13:10:34

+0

它與html添加到該函數。謝謝。 – firefiter 2012-01-03 13:13:17

1

檢查你的成功funcion,應該是這樣的:

 
success: function (response) {    
      //if process.php returned 1/true (send mail success) 
      if (response == "1") {     
       //hide the form 
       $('.form').fadeOut('slow');     

       $('.done').delay(1000).fadeIn('slow'); 

      }    
     }  
1

看起來你沒有返回從PHP腳本的JavaScript函數的響應。如果你爲你的成功功能做了如下的事情,它應該讓你走上正軌:

success: function(html) 
{ 
    if(html=='1') 
    { 
     [...] 
    } 
} 
相關問題