0

我有一个要传递给 Ajax 的全局变量。Ajax 对我来说很新,我已经做了一些研究和测试,但我被困住了。对于我的第一个问题,我不知道变量是否被传递到 Ajax 函数中。

我对 Json 并不是很感兴趣,但是我也确实尝试过,但它也不正确。

我不希望从 php 获得对页面的响应,该页面正在使用现有的 js 和 html 进行更新。

我的第二个难题是我的 php 文件应该在它应该被激活时被激活,但是它将 0 发布到数据库字段中。这里的另一个问题也是它正在将所有用户的钱更新到同一个 0 条目,所以它的 isset 设置还没有正确设置。我相信我的 bindValue 编码正确,我真的不确定是否需要将 POST 分解到 php 页面,如果需要,如果我必须使用该值,我该怎么做?此外,当我添加 WHERE userid = userid 来更新游戏时,游戏完全停止。

任何帮助,即使是一个小的修复都将不胜感激。

这是文件。提前感谢您帮助我了解 Ajax。

游戏.js

money = 2000;

function updateMoney() {
   if ( pot <= 0 ){ 
   if ( money <= 0 ){ 
   document.getElementById("aaa").innerHTML = "Lost? Here's A Loan !!!";
      money = 1000 ;}
  }
   document.getElementById("money").innerHTML = money;
  }
  




function enterWager(){  // doMath function inside here
var x=parseInt(document.getElementById('textbox').value,10);  // Displays the textbox for placing a 
wager
   if (money <= 0) {
      x = 0 ; }
document.getElementById("bet").innerHTML = parseInt(x,10);
if(isNaN(x)||x < 1 || x > 250)
{
document.getElementById("aaa").innerHTML = "You're Out Of Money!!!";
}
    document.getElementById("textbox").style.display = 'none';
    document.getElementById("button").style.display = 'none';
    
    


function doMath() {  //   PVPCoinTransaction()   and   
transferCoins()  are off right now. Plus 4 tests failed 
and 
are also off at end of function.

   if (pot == 0){
       countWagers = 0;
   }
   
   if (money <= 0) {
      money = 0 ; }
   if (x > money) {
      x = money ; }
  money = money - x;
  pot = pot + x;
                 }


doMath()


function updateDatabase() {   

// POST test not working
//    $.ajax({
//     url: 'php/credits/credit.php', // 
//     type: "POST",
//     dataType:'json', // add json datatype to get json
//     data: ({money: 145}),  Do I put div here and how?
//     success: function(data){
//   I dont need to return anything, just update db field!
//     }
//}); 


// this section reaches php but posts 0 into database field
//data = money // I don't think this is working.
if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
  xml = new XMLHttpRequest(); 
  } else if (window.ActiveXObject) { // IE 8 and older  
  xml = new ActiveXObject("Microsoft.XMLHTTP");  
  }
xml.open("POST", "../php/credits/credit.php", true);
xml.setRequestHeader("Content-type", "application/x- 
www-form-urlencoded");
xml.send(money);
}



updateMoney()
updateDatabase()

信用.php

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

if (isset($_SESSION['userid'])) {
//  $money = null;
//  $money = $_POST['money'];

try {
$db = DB();
header("Content-Type: application/json"); 

$stmt = $db->prepare("UPDATE usersystem SET money=:money");
$stmt->bindValue(':money', $_POST['money'], PDO::PARAM_STR);
$stmt->execute(); 

}
catch(PDOException $e)
{
    $db = null;  
    echo $e->getMessage();  
}
}
?>
4

3 回答 3

0

您的服务器需要一个money在您的$_POST阵列中调用的密钥。这意味着为了正确接收数据,您还需要使用密钥发送数据。在 PHP 中,这些数据看起来像一个关联数组,而在 JavaScript 中则像一个对象,既有键又有值。

完成键值结构的最简单方法是创建具有key=value结构的字符串。这类似于表单将数据发送到服务器的方式,并且不需要在后端进行修改即可接收数据。

var package = `money=${money}`;

没有任何问题XMLHttpRequest(有ActiveXObject;) ),我建议学习Fetch API。它是向服务器发出 HTTP 请求和从服务器发出 HTTP 请求的更新和简化规范。您已经表明您不需要接收响应,这意味着带有发送数据的基本 POST 请求如下例所示。

fetch('../php/credits/credit.php', {
  method: 'POST',
  body: package
});

第一个参数是 URL,第二个参数是选项对象。该method物业不言自明。该body属性是您发送的数据(类似于xml.send(package);)。

现在,如果您的 URL 正确,则应将带有正确数据的 HTTP 请求发送到您的服务器。

// $_POST should have received data, and because you've send it as a
// key-value it will be represented as an associative array with, 
// you guessed it, keys and values.
$money = $_POST[ 'money' ];

// $money will always be a string in this form, so you'll 
// need to convert it if you need it to be a number.
$money_as_number = intval( $money );

要测试这是否有效network,请在浏览器的开发人员工具中打开选项卡。您可以检查是否发生 HTTP 请求并检查是否已发送正确的有效负载。

于 2020-11-07T22:11:32.720 回答
0

好的,这就是控制台中的工作原理......

function updateDatabase() { 

var package = money;
console.log(package);
fetch('../php/credits/credit.php', {
  method: 'POST',
     headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
  body: JSON.stringify(package)

});
}

控制台日志 = 1975 game.js:1608:9

我现在只需要 1975 年的值通过我的 php 发布到数据库。它仍在将 0 发布到我的数据库中。

于 2020-11-08T18:04:53.823 回答
0

我解决了。感谢您让我走上正确的道路!

<?php 
session_start();
if (isset($_SESSION['userid'])) {
 $money = json_decode(file_get_contents('php://input'), true);
 $money_as_number = intval( $money );
 $userid = $_SESSION['userid'];
try {
$db = DB();

$stmt = $db->prepare("UPDATE usersystem SET money=:money WHERE userid=:userid");
$stmt->bindValue(':money', $money_as_number, PDO::PARAM_INT);
$stmt->bindValue(':userid', $userid, PDO::PARAM_STR);
$stmt->execute();

}
    catch(PDOException $e)
    {
        $db = null;  
        echo $e->getMessage();  
    }
    }
    ?>
于 2020-11-08T18:14:48.057 回答