I've been trying to parse my data for a few days now and still have no clue how to get the results from my PHP array that is encoded by using json_encode. I am new to JQuery.
this is not working:
$.post('coordinate_array.php',{},function(data) { //ERROR HERE EXPECTING SOMETHING??
results = JSON.parse(data);
for(i = 0;i < results.length;i++) {
Paint(results[i].x, results[i].y);
}
});
I'm getting my data from this php file:
<?php
include 'db_conn.php';
header('Content-Type: application/json'); //not sure if i need this here??
$coordinate_sql = "SELECT x_coord, y_coord FROM coordinates";
$result = mysqli_query($conn,$coordinate_sql);
//see if query is good
if($result === false) {
die(mysqli_error());
}
//array that will have number of desks in map area
while($row = mysqli_fetch_assoc($result)){
//get desk array count
$desk[] = array('x' => $row['x_coord'],
'y' => $row['y_coord']);
} //end while loop
echo json_encode($desk); //encode the array
?>
print(json_encode($desk));. Also,die(mysqli_error())isn't JSON, so if something goes wrong it will be invalid JSON... You may also have an error somewhere else in your PHP file causing some notice (or whatever) being printed before the JSON.json_encode()RETURNS the encoded string. it doesn't do output, so your script is effectively useless since nothing gets output to the client. You needecho json_encode($desk)