简体   繁体   English

ajax 响应只显示 jquery 和 php 中数据库的最后一行?

[英]ajax response display only last row from database in jquery and php?

I am working on php and jquery with one dropdown with companies.我正在使用公司的一个下拉列表处理 php 和 jquery。 i want to display particular companies all employees on select that company.我想在选择该公司时显示特定公司的所有员工。 for this i am using ajax response with jquery.为此,我将 ajax 响应与 jquery 一起使用。 my reponse shows me perfect data but my each function is not working correctly.我的回复显示了完美的数据,但我的每个功能都无法正常工作。 it shows me only last record from db.它只显示了来自 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>");

                      });
              },  
      });

My response shows me correct data.我的回答显示了正确的数据。 for eg where i am having 2 employees it shows me [object object][object object]例如,在我有 2 名员工的地方,它会向我显示[object object][object object]

if i am having 1 employee its shows me [object object].如果我有 1 名员工,它会向我显示[object object].

But its not working inside each function.但它不能在每个函数内工作。 inside each function it only shows last record from database.在每个函数中,它只显示数据库中的最后一条记录。

My html of company list:我的公司列表html:

<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>

code that will call after ajax response: ajax响应后调用的代码:

 <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>");

                      });
              },  
      });

Also you are appending directly to table.你也直接附加到表。 I prefer to append in tbody like below:我更喜欢在 tbody 中附加如下:

$(".bodytable").append("<tr><td>" + value.emp_name + "</td><td>"  + "</td><td><input type='file'></td></tr>");

Hope it helps you.希望对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM