所以我有一个PHP脚本,我问一个简单的查询,然后我把它放在一个数组中。
<?php
$query = mysql_query('SELECT ATX12V FROM results');
$resultSet = array();
while($row = mysql_fetch_array($query)){
$resultSet['ATX12V'] = $row['ATX12V'];
$data[] = $resultSet;
}
print json_encode($data);
?>
print json_encode($ data)的结果是:
[{ “ATX12V”: “10”},{ “ATX12V”: “65”},{ “ATX12V”: “64”},{ “ATX12V”: “96”}]
下面我有一个javascript代码,我的问题是如何将$ data添加到数据[] ??
<script>
var buyerData = {
labels : ["January","February","March","April","May","June", "July", "August"],
datasets : [
{
fillColor : "#9DB86D",
strokeColor : "#ACC26D",
pointColor : "#9DB86D",
pointStrokeColor : "#9DB86D",
data : []
}
]
}
</script>
答案 0 :(得分:2)
如何简单:
data : <?php echo json_encode($data); ?>
答案 1 :(得分:0)
你可以使用ajax,如:
(home.html做为):
<script type="text/javascript">
window.onload = function() {
$.ajax({
type: "POST",
url: "request.php",
dataType: "json",
success: function (data) {
alert(data[0].ATX12V);
}
});
}
</script>
<script type="text/javascript" src="js/jquery.js"></script>
和服务器端(request.php):
<?php
$query = mysql_query('SELECT ATX12V FROM results');
$resultSet = array();
while($row = mysql_fetch_array($query)){
$resultSet['ATX12V'] = $row['ATX12V'];
$data[] = $resultSet;
}
print json_encode($data);
?>
注意:在项目中添加jquery.js