【发布时间】:2015-04-20 10:01:16
【问题描述】:
我的服务器代码正在返回 json 数据,其中包含 select mysql 查询。现在我必须解析这些信息,我需要将 json 信息填充到表中,我将如何做到这一点?
我的服务器代码
<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","2323");
mysql_select_db("service");
if(isset($_POST['type']))
{
if($_POST['type']=="carpenter"){
$startDate=$_POST['startDate'];
$endDate=$_POST['endDate'];
$query="select * from booking where scheduledDate between $startDate AND $endDate";
$result=mysqi_query($query);
$count=mysql_num_rows($result);
$retVal=array();
while($row=mysqli_fetch_assoc($result)){
$$retVal[]=$row;
}
echo json_encode($retVal);
}
} else{
echo "Invalid Format";
}
我的脚本
<script>
function fetchData2(){
$(".data-contacts2-js tbody").empty();
var startDate=$('#datepicker1').val();
var endDate=$('#datepicker2').val();
$.ajax({
url: "http://localhost/service/cleaning.php",
type:"POST",
dataType:"json",
data:{type:"carpenter", startDate:startDate, endDate:endDate},
ContentType:"application/json",
success: function(response){
alert(obj);
},
error: function(err){
alert("fail");
}
});
}
$(document).ready(function(){
$(".data-contacts2-js tbody").empty();
$('#fetchContacts2').click(function() {
fetchData2();
});
});
</script>
我的 html 代码
<div class="block-content collapse in">
<div class="span12">
<table class="data-contacts2-js table table-striped" >
<thead>
<tr>
<th>ID</th>
<th>Customer Name</th>
<th>Customer Mobile</th>
<th>Customer Email</th>
<th>Address</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<button id="fetchContacts2" class="btn btn-default" type="submit">Refresh</button>
</div>
我的 Json 格式是
[
{
"b_id": "101",
"cust_name": "qwq",
"cust_mobile": "323232323",
"cust_email": "u@gmail.com",
"cust_address": "kslaksl",
"scheduledDate": "2015-02-26",
"scheduledTime": "14:30:00",
"sc_id": "3",
"sps_id": "1"
}
]
我的数据库表:
【问题讨论】:
-
您可以使用
$.each从响应中构建您的标记表行,然后只需在tbody上使用.html(markup)。这是错字吗?$result=mysqi_query($query); -
@Ghost,谢谢你的回复,你能解释更多吗..
-
你能显示你的 json 响应吗
-
@Outlooker,感谢您的回复,我添加了我的数据库快照,请查看,我是 JSon 的新手,我不知道如何响应..
-
@neelabhsingh 只需在成功块中使用该函数
$.each(response, function(index, element){ // build html here });并删除您不需要的ContentType:"application/json",
标签: php jquery mysql ajax json