2011-12-15 67 views
1

我想要一個ajaxed的表單,當用戶點擊提交時,它會將表單值提交給一個php文件,這裏沒有什麼特別的東西,那一點對我來說都很清楚。jQuery - 如何通過不同的步驟獲得響應?

現在我想從服務器有多個響應購物在響應div。

例如形式是寫帖子,然後我的PHP文件將發佈該職位5個不同的論壇,我想在結果中顯示它到底是什麼做這樣

發佈至論壇1 .. 。然後清除,並顯示發佈到論壇2 ...

我想顯示腳本當前正在做什麼生活在jquery輸出div和我的問題是如何做到這一點?請告訴我jQuery中的代碼要求以及PHP中的代碼要求。

在此先感謝您的幫助

+0

難道我得到這個權利:要攔截`form`的`submit`行動並從那裏執行一系列AJAX請求? – 2011-12-15 23:41:32

回答

0

這是應該做到這一點給你一個函數:

//'step' indicates the current iteration of the posting loop, and 'limit' tells it after how many iterations to stop. If you want to post to 5 of 5 forums, set 'step' to 1 and 'limit' to 5. 

function submitSteps(step, limit) 
{ 
    if (step <= limit) 
    { 
    $.ajax({ 
     url: 'http://example.com', 
     data: 'postTo='+step, 
     beforeSend: function() 
     { 
      if ($('#notice p:last').html().indexOf('problem') > -1) 
      { 
        $('#notice p:last').remove(); 
      } 
      $('#notice').append('<p>Posting to forum '+step+'...</p>'); 
     }, 
     success: function() 
     { 
      $('#notice p:last').remove(); 
      $('#notice').append('<p>Post to forum '+step+' complete!</p>'); 
      window.setTimeout('submitSteps(step+1, limit)', 1000); 
     }, 
     error: function() 
     { 
      $('#notice p:last').remove(); 
      $('#notice').append('<p>There was a problem posting to forum '+step+'.</p>'); 
      window.setTimeout('submitSteps(step, limit)', 1000);  
     } 
    } 
}