Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...