0

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.

1 Answer 1

0

Instead of print_r(), use json_encode() and send the output with a JSON content type header. Then use a JSON parser on the client side. E.g. jQuery auto-converts valid JSON to a JavaScript object for you if you use it's ajax functionality.

Unrelated: you will not be able (nor would you want) to use document.write(), for this (or just about anything!).

2
  • Hello Steve. From your response I guess I need to learn JSON and jQuery, and that's what I am working on now. May you be a little bit more explicit about your proposed solution to my problem? I would appreciate it so much. Commented Jul 14, 2013 at 10:49
  • 1
    I got it! I added these lines to download_gems.php: $x = json_encode($entries); echo $x; And this line to Game.html var objMisCoords = JSON.parse(miscoords); Now, I have the coords in the object "objMisCoords", ready to be used. Thanks a lot Steve !! Commented Jul 14, 2013 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.