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;
}
?>
'數據= { '動作':clickBtnValue};'應該只是'數據= {動作:clickBtnValue};' – larsAnders
你看了在AJAX請求/響應瀏覽器的開發者工具?是否有錯誤報告?你在網絡服務器上運行這個嗎? –
你有一個表單提交按鈕,然後你也通過ajax提交。這是多餘的。如果你不希望頁面刷新,那麼你可以殺死表單,但留下輸入字段,將信息收集到數據對象中,並通過ajax發佈。沒有必要的表格。 – nurdyguy