Good evening. I am creating a game using CANVAS, PHP, MySQL and AJAX. It is a very simple game: it consists of a board, 8 squares tall and 8 squares long (like that of a Chessboard). The user is supposed to click any of the squares, and the square he/she clicked is to be stored in a DataBase as (xpos, ypos). Of course, the DataBase is stored in the server-side and the html game is running on the client-side, so I need AJAX to interact between javascript and php. I did this job and everything worked fine.
My problem arises when I try to load the Board. Imagine we have in our DataBase the next data:
(Row) ... (xpos) ... (ypos)
1 1 2
2 3 2
3 6 5
When the user opens the Game, I need it to Load this positions onto the Board by retrieving the (xpos, ypos) data from the DataBase. The code I tried looks like this:
Game.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Basic Game</title></head>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded () {
requestXMLLoadGems();
}
function requestXMLLoadGems() {
var xmlhttp;
if ( window.XMLHttpRequest ) { // IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","download_gems.php", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
mycoords=xmlhttp.responseText;
document.write(mycoords);
}
}
xmlhttp.send(null);
}
</script>
<body></body>
</html>
The server-side file "download_gems" looks like this:
download_gems.php
<?php
// Open a MySQL connection
$dbinfo = 'mysql:host=localhost;dbname=mydb';
$user = 'root';
$pass = '';
$link = new PDO($dbinfo, $user, $pass) or die('Error connecting database');
// Create and execute a MySQL query
$sql = "SELECT xpos,ypos FROM board";
foreach($link->query($sql) as $row) {
$entries[]=array($row['xpos'], $row['ypos']);
}
print_r($entries);
?>
Everything works fine. Except that I need to retrieve Numerical Data (xpos, ypos), and not String data. My question is how can I retrieve NUMERICAL Data using:
xmlhttp.responseText;
I can't find the answer anywhere!
I would appreciate any help. Thank you very much.