I am encountering an issue with passing variables from PHP to JavaScript using JSON. The problem lies in the fact that I am able to debug and view the items in the responseText within my JavaScript, but I am unable to assign them to a variable or properly view them. Even when attempting to work with a single item, I have not been successful. Any insights as to why this could be happening would be greatly appreciated.
function ajaxrequestDB() {
var AJAX = null; // Initialization of the AJAX variable.
if (window.XMLHttpRequest) { // Checking for XMLHttpRequest object support.
AJAX=new XMLHttpRequest(); // Initializing it.
}
else { // Trying initialization IE style.
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
}
if (AJAX==null){
alert("Your browser doesn't support AJAX.");
return false
}
AJAX.onreadystatechange = function() {
if (AJAX.readyState==4 || AJAX.readyState=="complete")
{
callback(AJAX.responseText, AJAX.status);
}
}
var url='http://localhost/Scripts/refresh.php';
AJAX.open("GET", url, true);
AJAX.send();
alert(AJAX.responseText);
var result = AJAX.responseText;
eval(result);
}
Upon debugging on AJAX.responseText, the returned data from the PHP file can be observed, however, only a blank alert window appears when executing alert(AJAX.responseText).
Below is the PHP file responsible for retrieving data from the database and sending variables to JavaScript.
<?php
header('Content-type: application/json');
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cpd", $con);
$SQL = "SELECT name from markers";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)){
$data[]= $db_field;
}
echo json_encode($data);
?>