【发布时间】:2013-12-01 01:37:17
【问题描述】:
我正在学习将通过 PHP 从 MySQL 检索到的数据传递给使用 jQuery 的 HTML 表单。
php 文件返回两个数组变量中的所有数据:$resultsa 和 $resultsb
请随时纠正我的方法 - 我正在使用 .getjson 查询(但失败)来收集数据并将其存储在 JavaScript 变量中。
// retrievedata.php
<?php
include('/include/connection.php');
if (isset($getid))
{
$getid = $_GET['getid'];
}
$resultsa = array();
$resultsb = array();
$result = mysql_query("SELECT * FROM FB_FoodBrew");
while($row = mysql_fetch_array($result))
{
$resultsa[] = $row['Food_Name'];
$resultsb[] = $row['Drink_Varietal'];
}
echo json_encode(array_values($resultsa));
echo json_encode(array_values($resultsb));
javascript 粘贴在此处的 HTML 页面部分中:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script src="assets/flex-slider/jquery.flexslider-min.js"></script>
<script src="js/main.js"></script>
<script>
$.getJSON('page.php', function(data) {
// assuming data is an array
Array.forEach(function(item) {
console.log(item);
});
});
</script>
</head>
不幸的是,我在 javascript 方面的经验非常有限,而且我很难确定该脚本应该放在哪里、应该使用哪些变量等等。
【问题讨论】:
-
你可能已经看过了:api.jquery.com/jQuery.getJSON 它给出了一些关于如何打印一些数据的示例
标签: javascript php jquery getjson json