JsonResponse需要更新Javascript值并替换全局初始全局值-需要解决方案



我正试图了解fetch api和get。根据我的研究,这看起来很容易,但我的大脑并没有完全理解。在我的游戏Javascript中,它以money=2000全局变量开始。

游戏玩得很好,但当有人离开并转到他们的个人资料页面并返回游戏时,游戏会重置并从头开始游戏,并将钱重置回2000。

真正疯狂的是,如果我刷新页面,我最终会在网页上看到2200的正确值,这就是数据库中的值。

startgame.html

<!DOCTYPE html>
<html>
<head> 
<TITLE>Interactive Game</TITLE>
<meta charset="UTF-8">
</head>
<body background="images/pvpcoinnew.png" style="width=120%" onLoad="updateMoney(); popup('popUpDiv'); click(); updown()" oncontextmenu="return false;">
<img background="images/pvpcoinnew.png">
<select id="textbox">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25" selected>25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="500">500</option>
<option value="1000">1000</option>
</select> 
<button type="click" id="button" onclick="enterWager(); return false;">Wager!</button>
<p>Money: $<span id="money"></span><br></p>

<script type="text/javascript" src="game.js"></script>
</body>
</html>

下面的game.js文件从一开始就预设了2000年的货币。game.js

//money = 2000;
window.money = 2000;

fetch('../php/moneyupdate/moneyupdate.php?money=value')
.then((response) => {
return response.json()
})
.then((money) => {
// Work with JSON data here
window.money = money
console.log(money)
})
.catch((err) => {
// Do something for an error here
})


document.getElementById("money").innerHTML = window.money;

当php文件从获取中被触发时,状态为200。XHR响应有效载荷返回"0";2200〃;这是数据库中的内容,但网页仍显示2000。

这是moneyupdate.php文件

<?php 
session_start();
if(empty($_SESSION['userid']))
{
header("Location: ../login/index.php");
}
include('../../login/database.php');

if(isset($_GET["money"])) {
$money = htmlspecialchars($_GET["money"]);
$userid = $_SESSION['userid'];
try {
$db = DB();
$stmt = $db->prepare("SELECT money FROM usersystem WHERE userid=?");
$stmt->execute([$userid]);
$money = $stmt->fetchColumn();

// send a JSON encded money array to client
header('Content-type: application/json;charset=utf-8');
echo json_encode($money);
//}

}
catch(PDOException $e)
{
$db = null;  
echo $e->getMessage();  
}
}
// Notes: The json_encode function returns the JSON representation of the given value. 
//        The json_decode takes a JSON encoded string and converts it into a PHP variable.
?>

感谢您的帮助和回应!

当使用GET['mony']时,将其添加到您的url中"网址:site.com/page.php?货币=价值;。window.money=2000;

fetch('../php/moneyupdate/moneyupdate.php?money=2000')
.then((response) => {
return response.json()
})
.then((money) => {
// Work with JSON data here
//working with json data depends on structure 
// you can access data as money.money
window.money = money
console.log(money)
})
.catch((err) => {
// Do something for an error here
})

如果我理解正确,你可能想使用对象窗口来存储你的变量,并且你可以从的任何地方访问它

window.money = 2000;

fetch('../php/moneyupdate/moneyupdate.php')
.then((response) => {
return response.json()
})
.then((money) => {
// Work with JSON data here
window.money = money
console.log(money)
})
.catch((err) => {
// Do something for an error here
})

关于PHP部分,我希望我能提供帮助,但这不是我的强项。我希望我的小费对你有所帮助。

已解决-起初,我不确定该把getElementById放在哪里,没有意识到它需要在fetch中。我还将fetch放入一个函数中,这样就可以从html文件onload中调用它,我在html页面文件中显示了它。我还在updateMoney((中插入了它。它立即解决了我必须重新加载页面才能看到显示的货币价值的问题。

game.js

// window.money = 2000;
function getMoney(){
fetch('../php/moneyupdate/moneyupdate.php?money=value')
.then((response) => {
return response.json()
})
.then((money) => {
// Work with JSON data here
window.money = money
document.getElementById("money").innerHTML = window.money;
//console.log(money)
})
.catch((err) => {
// Do something for an error here
})
}
function updateMoney() {
if ( pot <= 0 ){ 
if ( money <= 0 ){ 
document.getElementById("aaa").innerHTML = "Lost? Here's A Loan !!!";
money = 1000 ;}
}
//       document.getElementById("money").innerHTML = money;
document.getElementById("money").innerHTML = window.money;
}

用JS编写上面的代码并不是我所需要做的全部。我必须实际触发getMoney((,这样它才能获取php文件并从数据库中获取列。

startgame.html

<body background="images/pvpcoinnew.png" style="width=120%" onLoad="getMoney(); updateMoney(); popup('popUpDiv'); click(); updown()" oncontextmenu="return false;">

我不需要更改php页面上的任何内容,但我仍然想知道我是否真的需要这一行,因为我没有用它做任何事情$money=htmlspecialchar($_GET["money"](;

moneyupdate.php

<?php 
session_start();
if(empty($_SESSION['userid']))
{
header("Location: ../login/index.php");
}
include('../../login/database.php');

if(isset($_GET["money"])) {
$money = htmlspecialchars($_GET["money"]);
$userid = $_SESSION['userid'];
try {
$db = DB();
$stmt = $db->prepare("SELECT money FROM usersystem WHERE userid=?");
$stmt->execute([$userid]);
$money = $stmt->fetchColumn();

// send a JSON encded money array to client
header('Content-type: application/json;charset=utf-8');
echo json_encode($money);
//}

}
catch(PDOException $e)
{
$db = null;  
echo $e->getMessage();  
}
}
// Notes: The json_encode function returns the JSON representation of the given value. 
//        The json_decode takes a JSON encoded string and converts it into a PHP variable.
?>

相关内容

最新更新