2012-12-19 45 views
3

我使用以下從Web應用程序發佈表單數據到服務器端PHP和快速庫。 fill.php處理簽名,將其附加到pdf中,並通過電子郵件發送結果。添加成功,以加倍jQuery/ajax後

一切工作正常,但我對這個部分沒有真正成功的功能:

$('#sendbtn').click(function(){ 

    signatures(); 

    $.post('https://www.quickbase.com/db/dbname?act=API_AddRecord', $('form').serialize()); 

    $.post('fill.php', $('form').serialize(), $.mobile.changePage('#successpop', {transition: 'pop', role: 'dialog'})); 

}); 

彈出顯示器,但它沒有任何成功的真正指標。在顯示成功彈出窗口之前,如何從這些帖子中的一個或兩個獲得迴應?有條件地從一個成功轉到另一個成功會很棒,但我不擔心QuickBase的帖子。在顯示成功之前,我肯定需要確認fill.php文章。

我喜歡這個的是它避免了重定向到我的應用程序之外的網址。

感謝您的任何幫助!

<form action="" encoding='multipart/form-data' encType='multipart/form-data'> 
<input type=text name=_fid_6 > 
<input type=text name=_fid_7 > 
<input type=text name=_fid_8 > 
<input type="hidden" name="img" id="img" /> 
<input type="button" value="Sign!" id="sendbtn" /> 
</form> 

     <script> 
      $(document).ready(function() { 
       $("#signature").jSignature() 
      }) 
      function signatures(){ 
       var $sigdiv = $("#signature"); 
       var datax = $sigdiv.jSignature("getData","image"); 
       $('#img').val(datax); 
       $sigdiv.jSignature("reset") 
      } 
     </script> 

<script> 
$(document).ready(function(){ 
    $('#sendbtn').click(function(){ 

     signatures(); 

     $.post('https://www.quickbase.com/db/dbname?act=API_AddRecord', $('form').serialize()); 

     $.post('fill.php', $('form').serialize(), $.mobile.changePage('#successpop', {transition: 'pop', role: 'dialog'})); 

    }); 
}); 
</script> 

<div data-role="page" id="successpop"> 
<div data-role="header" data-theme="c"> 
<h1>QAF Sent</h1> 
</div> 
<div data-role="content" data-theme="d">  
<h2>Your QAF has been sent</h2> 
<p><input type="button" data-inline="true" data-theme="b" value="Begin New QAF" data-icon="back" onclick="clearForm();"/></p> 
</div></div> 

回答

1

其中一個$ .post()將完成第一個,一個將完成第二個。你不一定知道是哪一個如此設置了一個全局變量(比如一個布爾值),它在第一個完成時被設置爲true,然後爲true添加一個檢查。當成功函數將該值視爲true時,它會調用onDoubleSuccess()函數。像這樣(demo):

<script> 
    $(document).ready(function(){ 
    $('#sendbtn').click(function(){ 
     var finishedFirstPost = false, 
     onDoubleSuccess = function() { 
     //code goes here 
     $.mobile.changePage('#successpop', {transition: 'pop', role: 'dialog'}); 
     }; 
     signatures(); 
     $.post(
     'https://www.quickbase.com/db/dbname?act=API_AddRecord', 
     $('form').serialize(), 
     function() { 
      if(finishedFirstPost) { 
      onDoubleSuccess(); 
      } else { 
      finishedFirstPost = true; 
      } 
     }); 
     $.post(
     'fill.php', 
     $('form').serialize(), 
     function() { 
      if(finishedFirstPost) { 
      onDoubleSuccess(); 
      } else { 
      finishedFirstPost = true; 
      } 
     }); 
    }); 
    }); 
</script> 

而且你不能做一個HTTP POST到外部域,而無需CORS support所以你應該Quickbase $.post()改爲$.get()。這是API documentation顯示此shoudl工作。

+0

太棒了!謝謝!如果任一帖子失敗,是否有方法顯示簡單的錯誤消息? – user1911141

+0

如果從$ .post()切換到功能更強大的[$ .ajax()](http://api.jquery.com/jQuery.ajax/) –

+0

,則可以隨時添加失敗函數。謝謝。我嘗試了$ .ajax() - 嘗試在1st的成功函數中嵌套第二篇文章,但它不起作用。很高興知道失敗並不適用$ .post() – user1911141