Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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

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

                      });
              },  
      });

My response shows me correct data. for e.g where i am having 2 employees it shows me [object object][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.

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>

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

Hope it helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...