2016-04-21 55 views
0

所以我在這裏問過類似的問題,然後得到了一個有效的答案,但是,在我的初學者項目和ajax的介紹進展後,我遇到了另一個問題。看起來,AJAX調用不成功..頁面重新加載後。在將更多文本字段添加到我的表單之前,我注意到了這個問題,但我無法想象這可能是如何相關的。後來的查詢依賴於表單是否成功提交,但我不想讓用戶刷新頁面,只是爲了將數據實際推送到數據庫。有什麼建議麼?jQuery AJAX調用失敗,直到刷新後

<form action="call.php" method="post"> 
    <input class="oomText" type="text" name="accountName" placeholder="Account Name"><br> 
    <h2 class="oomHead2">Email Notifcations For Phone Calls</h2> 
    <input class="oomText" type="text" name="accountEmailOne" placeholder="Email Recipient 1*"><br> 
    <input class="oomText" type="text" name="accountEmailTwo" placeholder="Email Recipient 2*"><br> 
    <input class="oomText" type="text" name="accountEmailThree" placeholder="Email Recipient 3*"><br> 
    <h2 class="oomHead2">Text Notifcations For Phone Calls</h2> 
    <input class="oomText" type="text" name="accountTextOne" placeholder="Text Recipient 1*"><br> 
    <input class="oomText" type="text" name="accountTextTwo" placeholder="Text Recipient 2*"><br> 
    <input class="oomText" type="text" name="accountTextThree" placeholder="Text Recipient 3*"><br> 
    <small>*Fields are <b>not</b> required to be filled out</small><br> 
    <input class="oomButton submit" type="submit" name="submit" value="submit"> 
    <script> 
     $(document).ready(function(){ 
      $('.oomButton.submit').click(function(){ 
       var clickBtnValue = $(this).val(); 
       var ajaxurl = 'http://clients.oomdo.com/provision/ajax.php', 
       data = {'action': clickBtnValue}; 
       $.post(ajaxurl, data, function (response) { 
       alert("Account Created"); 
       }); 
      }); 
     }); 
    </script> 
    </form> 

..Later在PHP文件

if ($_SESSION['submitted'] == true){ 
    // do something 
    } 

ajax.php

<?php 
     session_start(); 
     if (isset($_POST['action'])) { 
      switch ($_POST['action']) { 
       case 'submit': 
        submit(); 
        echo 'Hurrah'; 
      } 
     } 

     function submit() { 
      $_SESSION['submitted'] = true; 
     } 

    ?> 
+1

'數據= { '動作':clickBtnValue};'應該只是'數據= {動作:clickBtnValue};' – larsAnders

+0

你看了在AJAX請求/響應瀏覽器的開發者工具?是否有錯誤報告?你在網絡服務器上運行這個嗎? –

+0

你有一個表單提交按鈕,然後你也通過ajax提交。這是多餘的。如果你不希望頁面刷新,那麼你可以殺死表單,但留下輸入字段,將信息收集到數據對象中,並通過ajax發佈。沒有必要的表格。 – nurdyguy

回答

0

我有點生疏所以裸陪我,但我認爲你忘記了AJAX是異步的,這意味着在那裏發生的事情直到後來纔會發生。

因此,在您的示例中,您完成了AJAX調用,然後檢查了會話,然後調用完成了其功能(由於發送的數據量更高而花費更長時間)。這就是爲什麼刷新後會看到新的價值。

你有3個選擇這裏:

  1. 附上//do something聲明到您的success功能。

代碼示例:

$.post(ajaxurl, data, function (response) { 
    alert("Account Created"); 

    if ($_SESSION['submitted'] == true){ 
     // do something 
    } 
}); 
  • 創建函數和調用在你success函數的末尾。
  • 代碼示例:

    function sessionCheck(){ 
        if ($_SESSION['submitted'] == true){ 
         // do something 
        } 
    } 
    
    .... 
    
    $.post(ajaxurl, data, function (response) { 
        alert("Account Created"); 
    
        sessionCheck(); 
    }); 
    
  • 給您$(document)一個.ajaxsuccess(function)。這會觸發,每次你有一個Ajax調用完成。 請注意,這可能會覆蓋它們的多個實例在這種情況下,您必須爲每個.php輸出添加一個識別會話變量。
  • 代碼示例

    $(document).ajaxSuccess(function() { 
    
        if ($_SESSION['submitted'] == true){ 
         // do something 
         //then find a way to remove $_SESSION['submitted'] 
         //  maybe using AJAX as well. 
        } 
    
        //else if ($_SESSION['somethingElse'] == true) 
    });