ajax响应仅显示jquery和php中数据库的最后一行? [英] ajax response display only last row from database in jquery and php?
问题描述
我正在与一家公司合作开发php和jquery.一世 想要显示特定公司的所有雇员 公司.为此,我正在使用带有jquery的ajax响应.我的回应 向我显示了完美的数据,但是我的每个功能均无法正常工作. 它只显示了数据库中的最后一条记录.
I am working on php and jquery with one dropdown with companies. i want to display particular companies all employees on select that company. for this i am using ajax response with jquery. my reponse shows me perfect data but my each function is not working correctly. it shows me only last record from db.
$.ajax({
method: "GET",
dataType: 'json',
url:"getdata.php?id="+emp_id,
success:function (response){
$.each(response, function( index, value ) {
$(".bodytable").empty();
$("table.table").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
});
},
});
我的回答告诉我正确的数据.
例如,如果我有2名员工,则显示我[object object][object object]
My response shows me correct data.
for e.g where i am having 2 employees it shows me [object object][object object]
如果我有1名员工,则显示[object object].
if i am having 1 employee its shows me [object object].
但是它不能在每个函数中使用.在每个函数中,它仅显示数据库中的最后一条记录.
But its not working inside each function. inside each function it only shows last record from database.
我的公司列表的html:
My html of company list:
<select id="billing_com" name="billing_com" >
<option>--Select Customer--</option>
<?php foreach($com_data as $key=>$eachcomData){
?>
<option value="<?php echo $eachcomData['com_id'];?>" <?php
if(isset($_GET['id'])){ echo ($upload['com_name'] == $eachcomData['com_id'])?'selected=selected':'abc'; } ?> >
<?php echo $eachcomData['com_name']; ?>
</option>
<?php }?>
</select>
将在ajax响应后调用的代码:
code that will call after ajax response:
<table class="tabledata">
<thead>
<tr>
<th>Employee Name</th>
<th>Attach Payslip</th>
</tr>
</thead>
<tbody class="bodytable">
</tbody>
</table>
推荐答案
您应该在每个循环之外清空表.像这样:
You should make empty your table outside the each loop. Like this:
$.ajax({
method: "GET",
dataType: 'json',
url:"getdata.php?id="+emp_id,
success:function (response){
$(".bodytable").empty();
$.each(response, function( index, value ) {
$("table.table").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
});
},
});
您也将直接附加到表.我更喜欢将tbody附加如下:
Also you are appending directly to table. I prefer to append in tbody like below:
$(".bodytable").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
希望它对您有帮助.
这篇关于ajax响应仅显示jquery和php中数据库的最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!