Thought as this had to do with PHP as well, would be better in here than in the ClientSide Code area.
Basically, I have a php page which Im trying to use to send 3 requests using AJAX through to another script. Here are the two scripts, firstly the one that shows the output etc and sends the requests, and secondly the one which performs the backend database operations etc
(ajax1.php)
<html>
<head>
<title>AJAX Test #1 </title>
<head>
<script type="text/javascript">
function updateResources()
{
var xmlhttpmetal;
var xmlhttpcrystal;
var xmlhttpdeuterium;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttpmetal=new XMLHttpRequest();
xmlhttpcrystal=new XMLHttpRequest();
xmlhttpdeuterium=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttpmetal=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttpcrystal=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttpdeuterium=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttpmetal.onreadystatechange=function()
{
if (xmlhttpmetal.readyState==4 && xmlhttpmetal.status==200)
{
document.getElementById("metal").innerHTML=xmlhttpmetal.responseText;
}
}
xmlhttpcrystal.onreadystatechange=function()
{
if (xmlhttpcrystal.readyState==4 && xmlhttpcrystal.status==200)
{
document.getElementById("crystal").innerHTML=xmlhttpcrystal.responseText;
}
}
xmlhttpdeuterium.onreadystatechange=function()
{
if (xmlhttpdeuterium.readyState==4 && xmlhttpdeuterium.status==200)
{
document.getElementById("deuterium").innerHTML=xmlhttpdeuterium.responseText;
}
}
xmlhttpmetal.open("GET","ajax1script.php?r=metal",true);
xmlhttpmetal.send();
xmlhttpcrystal.open("GET","ajax1script.php?r=crystal",true);
xmlhttpcrystal.send();
xmlhttpdeuterium.open("GET","ajax1script.php?r=deuterium",true);
xmlhttpdeuterium.send();
setTimeout("updateResources()",1000);
}
</script>
</head>
</head>
<body onload="updateResources()";>
Metal: <span id="metal"></span> Crystal: <span id="crystal"></span> Deuterium: <span id="deuterium"></span>
<br /> <br /><?php echo time(); ?>
(ajax1script.php)
<?php
// Define resource per hour gains
$mph = 36000;
$cph = 1800;
$dph = 900;
// Other defines
$playerID = 1;
$conn = mysql_connect("localhost", "xxxxxxxxx", "xxxxxxxx") or die("Cannot connect " . mysql_error());
mysql_select_db("xxxxxxxxx", $conn);
$q = "SELECT * FROM ajax1 WHERE playerID = '$playerID'";
$result = mysql_query($q,$conn);
$row = mysql_fetch_assoc($result);
$currentMetal = $row['metal'];
$currentCrystal = $row['crystal'];
$currentDeuterium = $row['deuterium'];
$lastUpdate = $row['lastUpdate']; // unix timestamp
$now = time();
$timeSinceUpdate = $now - $lastUpdate; // seconds
if($_GET['r'] == "metal")
{
$metalGained = $timeSinceUpdate * ($mph / 3600);
$newMetal = $currentMetal + $metalGained;
$q = "UPDATE ajax1 SET metal = '$newMetal', lastUpdate = '$now' WHERE playerID = '$playerID'";
mysql_query($q,$conn);
echo $newMetal;
}
elseif($_GET['r'] == "crystal")
{
$crystalGained = $timeSinceUpdate * ($cph / 3600);
$newCrystal = $currentCrystal + $crystalGained;
$q = "UPDATE ajax1 SET crystal = '$newCrystal', lastUpdate = '$now' WHERE playerID = '$playerID'";
mysql_query($q,$conn);
echo $newCrystal;
}
elseif($_GET['r'] == "deuterium")
{
$deuteriumGained = $timeSinceUpdate * ($dph / 3600);
$newDeuterium = $currentDeuterium + $deuteriumGained;
$q = "UPDATE ajax1 SET deuterium = '$newDeuterium', lastUpdate = '$now' WHERE playerID = '$playerID'";
mysql_query($q,$conn);
echo $newDeuterium;
}
else
{
echo "Error";
}
?>
The basic idea is that in the database, for each player (there is only one for testing) it keeps their current amount of each of 3 resources, and the unix timestamp of when it was last updated. The idea is whilst a player is logged in and actively browsing, each second the script is run to update their total using the formulae above, and the database is updated etc etc.
When I first tried this, only using a single httprequest, (for metal) it worked perfectly, and in essence I could see the metal count ticking up 10 each second (as intended).
However I have tried to add two extra httprequests, made sure it all duplicates the first request but with a name change for all appropriate variables, however these last two dotn seem to work, leading me to believe you can only have 1 request at a time?
What would be the best way of handling this please? If I was limited to one request, how would it be best to return the data from the script so I could split it up and plonk it into the 3 appropriate <spans>?
Any thoughts on this would be hugely appreciated! 🙂