打印时数组值返回为“未定义”,即使通过使用print_r($items)
我可以看到数组不为空。这里可能是什么问题?
MySQL:
名为“nights”的表,列名为“name”、“price”、“day”、“queue jump”、“closures”和“doors”。他们都是人口稠密的。
事件信息.php:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
include('functions.php');
connect();
$night = $_POST['club'];
// this echos fine
$night = mysql_real_escape_string($night);
$query = "SELECT * FROM nights WHERE name = '$night'";
$result = mysql_query($query);
$items = array();
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array("price"=>$row['price'], "day"=>$row['day'], "queue jump"=>$row['queue jump'], "closing"=>$row['closing'], "doors"=>$row['doors']);
}
}
mysql_close();
// convert into JSON format and print
echo json_encode($items);
?>
JS:
<script type="text/javascript">
$(document).ready(function() {
// for simplicity's sake, but ultimately I'd like to load all the information
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
$('#club').change(function(event) {
$.ajax({
type: "post",
url: "eventinfo.php",
data: $(this).serialize(),
success: function(data) {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2><p>Day: ' + data.day + '</p>');
},
dataType: "json"
});
});
</script>