I have a simple PHP code that retrieves data from the table including image and stores it in a JSON object.
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selected = mysql_select_db("req2",$con)
or die("Could not select req2");
$table_first = 'locationtab';
$query=mysql_query("SELECT Id,City,Image FROM $table_first");
while ($row=mysql_fetch_assoc($query)) {
$array=$row;
$array['Image']=base64_encode($row['Image']);
$output[]=$array;
}
echo json_encode($output);
mysql_close($con);
?>
This works perfectly fine and I'm able to view the JSON object using print. on the client side, I wrote a Javascript+Ajax code to call this PHP file and on success display the data in the JSON object. I dont know what is missing in the below code but I don't seem to get the AJAX part to work at all. PLease so help
<html>
<head>
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$.ajax({
url:"table.php",
dataType:'json',
type:'POST',
success:function(output) {
alert( output.Id );
}
});
});
</script>
</head>
<body>
<p>test</p>
</body>
</html>
output
is an array of objects with ids.
You are treating it as a single object with an id.
You need either a for
loop, or to grab a specific index.
alert( output[0].Id );