比方说你有数据库表:
order(orderid, date, total)
order_details(id, orderid, products)
比你的index.php文件可能是这样的:
<?php
switch($_POST['action']) {
case 'get_orders':
/* you need to create this function (func_query), it would return an array of MySQL result set */
$orders = func_query("select * from orders");
header('Content-Type: application/json');
echo json_encode($orders);
exit();
break;
case 'get_order_details':
$order_details = func_query("select * from orders_details where orderid=" . mysql_real_escape_string($_POST['orderid']));
header('Content-Type: application/json');
echo json_encode($order_details);
exit();
break;
};
?>
<!doctype html>
<html>
<head>
<title>Orders</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('ready', function() {
$.ajax({
url: 'index.php',
type: 'post',
data: {'action': 'get_orders'},
dataType: 'json',
success: function(data) {
$('.orders_list').html('');
var orders_list_html = '';
if (data.length>0) {
orders_list_html += '<ul>';
$.each(data, function(i, order) {
orders_list_html += '<li data-orderid="' + order.orderid + '">' + order.date + ', ' + order.total + '</li>';
});
orders_list_html += '<ul>';
$('.orders_list').html(orders_list_html);
};
}
});
$('.orders_list').on('click', 'li[data-orderid]', function(e) {
var thisList = $(this);
var orderid = thisList.data('orderid');
$.ajax({
url: 'index.php',
type: 'post',
data: {'action': 'get_order_details', 'orderid': orderid},
dataType: 'json',
success: function(data) {
/* Do something with data, append to thisList or show it somewhere */
}
});
});
});
</script>
</head>
<body>
<h1>Your orders</h1>
<div class="orders_list"></div>
</body>
</html>
没有测试它,但你可以得到的想法。
对于前端,我将开始在看网页形式http://www.w3schools.com/html/html_forms.asp – 2014-10-27 23:18:26
当你列出的订单,它通常是不超过30,这样下去,并收集所有的订单详细信息和订单 - 根本不需要使用ajax。最后,隐藏订单详细信息并单击显示它们。 – skobaljic 2014-10-27 23:27:37