简体   繁体   中英

How do I update my php variables in javascript with updated mysql database information?

I have a database which is updating values, which are positions of an opponent in a multiplayer game.

My problem occurs in trying to update the opponent models with their updated position information.

I have two seperate php files to do this:

1.) update.php is called within the javascript file multiple times with

xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', "xml_http_request.php?mod0=" + truckHeading + "&mod1=" + newhtr[1] + "&mod2=" + absRoll + "&lla0=" + lla[0] + "&lla1=" + lla[1] + "&lla2=" + lla[2] + "&pid=" + '1' + "&rangeCheck=" + rangeCheck + "&ranger=" + ranger + "&namely=" + namely + "&message=" + message + "&unLoader=false", true); xmlhttp.send(null);

2.) retrieve.php is only called once in my index.php file

I believe the solution is to run the retrieve.php file once every time the update.php file is run (say right after the above code), but I don't know what the line of code is to do that.

Here's what's in my retrieve.php file:

require("db1.php"); //for using live public database

$query="SELECT mod0, mod1, mod2, lla0, lla1, lla2, namely, message FROM positioner WHERE id = 1 ";
$result=mysql_query($query);

while ($row=@mysql_fetch_assoc($result)){

    $mod0 = $row['mod0'];
    $mod1 = $row['mod1'];
    $mod2 = $row['mod2'];
    $lla0 = $row['lla0'];
    $lla1 = $row['lla1'];
    $lla2 = $row['lla2'];

}

echo "<script> var mod0php = $mod0; </script>";
echo "<script> var mod1php = $mod1; </script>";
echo "<script> var mod2php = $mod2; </script>";
echo "<script> var lla0php = $lla0; </script>";
echo "<script> var lla1php = $lla1; </script>";
echo "<script> var lla2php = $lla2; </script>";

?>

If these are numbers, then I don't see a problem. but if they are strings, I see a problem at

echo "<script> var mod0php = $mod0; </script>";
echo "<script> var mod1php = $mod1; </script>";
echo "<script> var mod2php = $mod2; </script>";
echo "<script> var lla0php = $lla0; </script>";
echo "<script> var lla1php = $lla1; </script>";
echo "<script> var lla2php = $lla2; </script>";

change this to

echo "<script> var mod0php = '$mod0'; </script>";
echo "<script> var mod1php = '$mod1'; </script>";
echo "<script> var mod2php = '$mod2'; </script>";
echo "<script> var lla0php = '$lla0'; </script>";
echo "<script> var lla1php = '$lla1'; </script>";
echo "<script> var lla2php = '$lla2'; </script>";

you are missing qoutes around the strings.

you would run it by

include 'retrieve.php';
or include_once 'retrieve.php';

however, this incurs a security risks. another method is to have php retrieve it using http.

$content = http_get("http://nowhere.com/retrieve.php"); //there are options on this call if you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM