【发布时间】:2016-04-26 15:44:10
【问题描述】:
我无法找到我的问题的答案...事实是我对 jQuery 和 JSON 不熟悉。
在登录页面上,我想在页面加载时显示顶级客户和顶级曲目。 我通过 php 的 echo 显示它们,但我想创建 JSON 对象并将其发送到 login ...循环并在无序列表中显示。
你们能帮我创建 JSON 并在 jquery 中显示它吗?
这是我的代码:
jQuery:
/* Function to load top Customers */
function loadCustomers() {
/* Create data string to call functions in php*/
var dataString ="function1=loadCustomers&function2=loadTracks";
$.ajax({
type:"GET",
url: "login.php",
data: dataString,
dataType: "json",
success: function(data) {
$("#error").show();
$("#errormsg").html(data.FirstName);
//how can i display Json data in unordered list ? #customerList
},
error: function() {
$("#error").show();
$("#errormsg").text("cant display data");
}
});
}
PHP:
}else if ($_SERVER["REQUEST_METHOD"] == "GET") {
//JSON customers array
$customers = array();
if ($_GET["function1"] == "loadCustomers") {
try {
$customerStmt = $conn->prepare("Select customer.FirstName, customer.LastName, customer.City, sum(invoice.Total) from invoice INNER JOIN customer on invoice.CustomerId = customer.CustomerId group by invoice.CustomerId order by sum(invoice.total) DESC LIMIT 5");
$customerStmt->execute();
$customerRows = $customerStmt->fetchAll();
//how to create JSON data to send it
header("Content-type: application/json");
echo json_encode($customerRows);
}catch (PDOException $e) {
$e->getMessage();
}
}
【问题讨论】:
标签: javascript php jquery mysql json