[英]jQuery AJAX call failing until after refresh
So I've asked a similar question here before and got an answer that worked, however, after progressing in my beginner project and introduction to ajax, I've come across another problem. 因此,我之前在这里问过类似的问题并获得了有效的答案,但是,在我的初学者项目和ajax入门之后,我遇到了另一个问题。 It seems that the AJAX call isn't successful..until after the page is reloaded.
直到页面重新加载后,AJAX调用似乎没有成功。 I noticed this issue prior to adding more text fields to my form, but I can't imagine how that could be related.
在向表单中添加更多文本字段之前,我注意到了这个问题,但是我无法想象这之间的关系。 A later query is reliant on whether the form is successfully submitted, but I don't want to make the user refresh the page just for that data to actually be pushed through to the database.
以后的查询依赖于表单是否成功提交,但是我不想让用户刷新页面只是为了将数据实际推送到数据库中。 Any suggestions?
有什么建议么?
<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 on in the php file ..稍后在php文件中
if ($_SESSION['submitted'] == true){
// do something
}
ajax.php ajax.php
<?php
session_start();
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'submit':
submit();
echo 'Hurrah';
}
}
function submit() {
$_SESSION['submitted'] = true;
}
?>
I think you've forgotten that AJAX is asynchronous, meaning what happens in there doesn't happen until later. 我认为您已经忘记了AJAX是异步的,这意味着直到后来才发生。
So, in your example, you've done the AJAX call, THEN checked your session, THEN the call finished its function (taking longer because of the higher amount of data sent). 因此,在您的示例中,您完成了AJAX调用,然后检查了您的会话,然后调用完成了其功能(由于发送的数据量更多,因此花费了更长的时间)。 That's why it sees the new value after the refresh.
这就是刷新后看到新值的原因。
You have 3 options here: 您在这里有3个选择:
//do something
statement into your success
function. success
函数中包含//do something
语句。 Code sample: 代码示例:
$.post(ajaxurl, data, function (response) {
alert("Account Created");
if ($_SESSION['submitted'] == true){
// do something
}
});
success
function. success
函数的结尾处调用它。 Code sample: 代码示例:
function sessionCheck(){
if ($_SESSION['submitted'] == true){
// do something
}
}
....
$.post(ajaxurl, data, function (response) {
alert("Account Created");
sessionCheck();
});
$(document)
an .ajaxsuccess(function)
. $(document)
一个.ajaxsuccess(function)
。 This will trigger, each time you have an ajax call completed. Code Sample 代码样例
$(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)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.