微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将全局变量传递给ajax函数,发布到php,保存到数据库

如何解决将全局变量传递给ajax函数,发布到php,保存到数据库

我有一个想要传递给Ajax的全局变量。 Ajax对我来说还很陌生,我已经进行了一些研究和测试,但是我陷入了困境。我不知道第一个问题是否将变量传递给Ajax函数

我对Json并不真正感兴趣,但是我也尝试过这样做,这也不对。

我不希望从PHP返回页面,该页面正在使用现有的js和html进行更新。

我的第二个难题是我的PHP文件在应有的状态下被激活,但是它在数据库字段中张贴了0。这里的另一个问题是,它会将所有用户的钱更新为相同的0条目,因此尚未正确设置其币种设置。我相信我的bindValue编码正确,我真的不确定是否需要将POST分解到PHP页面,如果需要,则必须使用该值,我该怎么做?同样,当我添加WHERE userid = userid来更新时,游戏完全停滞了。

任何帮助,即使是很小的修复,也将不胜感激。

这是文件。预先感谢您帮助我了解Ajax。

game.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()

credit.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();  
}
}
?>

解决方法

您的服务器在您的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请求,并检查是否已发送正确的有效载荷

,

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

#include <stdlib.h>

#include "allocation.h"

int *arithmeticSequence(int count,int initValue,int increment)
{
    int *table = NULL;

    table = malloc(count * sizeof(int));

    if(table == NULL)
        exit(1);

    for(int i=0 ; i < count ; i++)
    {
        table[i] = initValue + i * increment;
    }

    printf("Adresse de l'element 0 de la table : %p\n",&table[0]);

    return &table[0];

}

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

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

,

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

npm install --arch=x64 --platform=linux sharp

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。