I've got a an AJAX page request that uses a php file designed to handle queries to my MySQL database. The php file works just fine, but for some reason it's not being nice with me. Here's what I have:
function updateForm(){
ID = $('#listings').val();
$.ajax({
type: "POST",
url: 'query.php',
data: "query=true" +
"&id=" + ID,
datatype: 'json',
success: function(data) {
alert(data);
updatePreview();
}
});
}
gives me a popup with:
{"results":[{"ID":"12","area":"Hoboken","bedrooms":"5","fullbath":"3","halfbath":"1","remarks":"No remarks to speak of.","sqft":"2500","photos":null,"price":"1000","fee":null,"realtor":"Jane Doe","phone":"555-555-5555","address":"10th & Willow","bix":"1"}]}
but as soon as I change it to:
function updateForm(){
ID = $('#listings').val();
$.ajax({
type: "POST",
url: 'query.php',
data: "query=true" +
"&id=" + ID,
datatype: 'json',
success: function(data) {
alert(data.results);
updatePreview();
}
});
}
the popup just says undefined.
Ultimately, I want to parse out information and update my page accordingly, but I can't seem to access any of the properties of this JSON object. What's going on?
EDIT:
Here's the code from the php file:
if (isset($_POST['query'])){
if (isset($_POST['id'])){
$query = 'SELECT * FROM bix WHERE ID=' . get_post('id');
$listing = mysql_query($query);
print_json($listing);
}
}
function print_json($var){
$output = array();
while($row = mysql_fetch_assoc($var)) {
$output["results"][] = $row;
}
echo json_encode($output);
}
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}