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