gpt4 book ai didi

php - 如何让ajax延迟请求url

转载 作者:行者123 更新时间:2023-11-29 21:10:13 26 4
gpt4 key购买 nike

我为我的项目做了一个作业。我必须使用ajax来更新mysql而不刷新网页。

我有2个php文件,第一个是a.php代码如下(查看用户信用):

if($_GET['do'] == 'viewcredit'){
$quser = DB::query("SELECT * FROM ".DB::table('game_jnship_user')." WHERE uid = '$_G[uid]'");
$ruser = DB::fetch($quser);
//$moneyN = $ruser['money'];
echo $ruser['money'];
}

第二个 php 文件是 b.php代码如下(执行一些操作):

$quser = DB::query("SELECT * FROM ".DB::table('game_jnship_user')." WHERE uid = '$_G[uid]'");
$ruser = DB::fetch($quser);
$moneyN = $ruser['money'];
$gcoin = $ruser['gcoin'];
if($_GET['do'] == 'treasurehunt'){
$moneyadd = $moneyN + 1;
DB::query("UPDATE ".DB::table('game_jnship_user')." SET money = '$moneyadd' WHERE uid = '$_G[uid]'");
}

我的html代码如下:

<script src="https://code.jquery.com/jquery-1.12.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>jQuery.noConflict();</script>
<script>
jQuery(function() {
jQuery(".submit_button").click(function() {
var url = "a.php?do=viewcredit";
var decodedUrl = decodeURIComponent(url);
jQuery.ajax({
type: "POST",
url: decodedUrl,
cache: true,
success: function(html){
jQuery("#money").html(html);
}
});
});
});
</script>
<div id="money">$ruser[money]</div>
<a href="javascript:;" onclick="showWindow('jn_spop', 'b.php?do=treasurehunt')" class="c_button submit_button"><strong>Confirm</strong></a>

所以,现在当我单击 html 的最后一行时,<a>Confirm</a> ,首次出现 <div id="money">$ruser[money]</div>不会给出最新结果。

好吧,假设 $ruser[money] = '1'; ,在我点击 <a>Confirm</a> 后,mysql已更新money2 ,但是<div id="money">$ruser[money]</div>仍然显示 1。单击 <a>Confirm</a> 后再次,<div id="money">$ruser[money]</div>显示2 (但是mysql已经更新money3。)

所以,如果我想要 ajax延迟执行a.php?do=viewcredit , 是否可以?有没有其他办法我可以获得最新的$ruser[money]无需刷新网页?

谢谢。

最佳答案

您可以设置 async: false。但是,如果您的 ajax 请求需要时间显示一些加载程序,那么用户就会了解正在执行某些操作。欲了解更多详情,请查看此链接:http://api.jquery.com/jquery.ajax/

jQuery.ajax({
type: "POST",
url: decodedUrl,
cache: true,
async: false,
success: function(html){
jQuery("#money").html(html);
}
});

关于php - 如何让ajax延迟请求url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com