0

I am coding an application which fetches the data from data base on the basis of id given and return row in table, it is a bar code billing module, it .append the row as the barcode string given, but the problem which I am facing, when I am returning table row as a HTML code <tr>..</tr>, via ajax I cannot see the new table row printed(added) on my table.

Here is my view:

 <table class="table table-bordered">


              <thead>
                <tr>
                  <th>S No.</th>
                  <th>Particular</th>
                  <th>QTY</th>
                  <th>Cost</th>
                <th>Discount</th>
                <th>Discard</th>


                </tr>
              </thead>

               <tbody class="todo_list">
               <?php echo $list;?>
             <input type="hidden" name="bill_ref" value="<?php echo $bill_ref ?>"/>
             <input type="hidden" name="date" value="<?php echo $date ?>"/>

               </tbody>
               <?php echo $total_bill; ?> 

                <div id="bill_tbl">
                      <--!MY NEW ROW SUPPOSED TO BE ADDED HERE!-->
            </div>
            </table>

My ajax JS:

 <script type="text/javascript">
$(document).ready(function(){


               $("#c_id").change(function(){
                     var send_data=$("#c_id").val();
                     $.ajax({
                        type:"post",
                        url:"billing_table",
                        dataType: "html",

                        data:"i_id_post="+send_data,
                        success:function(data){
                              $("#bill_tbl")..after(data);

                        }

                     });
               });
           });
</script>

and my controller (for testing purpose i am operating my DB part in controller i am sorry for this):

public function billing_table()
    {
    $b_barcodestring=$this->input->post('b_barcodestring');
    $i_id=$this->input->post('i_id_post');
    if($i_id==''&& $b_barcodestring!='')
    {

    echo $b_barcodestring;

    }
    else if($b_barcodestring=='' && $i_id!='' )
    {


    //i want to print sumple this dummy row in my table
    echo '<tr ><input type="hidden" name="counter[]" value="ssss"></tr><tr ><td>+counter+</td>  <input type="hidden" name="particular[]" value="Greting Card" ><td>'.$i_id.'</td>  <td>  <input type="number" name="qty[]" value="+qty_cal+"></td> <td> <input type="hidden" name="cost[]" value="150" >150</td> <td><input type="hidden" name="discount[]" value="N/A">N/A</input></td> <td ><button class="btn btn-danger"><i class="icon-remove icon-white"></i>Delete</button></td></tr>';
    }

    $this->output->enable_profiler(TRUE);
    }

Here is the FIDDLE.

5
  • instead of $("#bill_tbl")..after(data) try $("#bill_tbl").after(data) Commented Aug 5, 2013 at 8:53
  • @VinodLouis : ya i tried extra " . " was my typing mistake , its still not working Commented Aug 5, 2013 at 8:56
  • I don't think you can add a row to a table outside tbody or thead, i mean I think it won't be w3c compliant, but not sure if it's the origin of the issue. You should really consider review your output layout imho. Commented Aug 5, 2013 at 8:58
  • First check the received data in success method. simply alert that. if there was an error, make sure that you are not using CodeIgniter CSRF prevention feature. If so, just do a little search on SO to find the solution. Commented Aug 5, 2013 at 9:10
  • can it be done with JSON?? how? Commented Aug 5, 2013 at 10:15

3 Answers 3

4

Change the following line in the JS function

success:function(data){
      //$("#bill_tbl")..after(data);
      $(".table").append(data);
}

Remove the div#bill_tbl from the table

Sign up to request clarification or add additional context in comments.

3 Comments

have you checked that you are getting the correct response from ajax? Checked your console for the response you are getting?
what you have done in your jsfiddle, change to $(".todo_list").append(data);
Happie to help :D @user2652367
0

It should be

$("#c_id").change(function(){

    var send_data=$("#c_id").val();
    var b_barcodestring = $("#b_id").val(); //your barcodestring , I just supplement it cannot find where you getting it

    $.ajax({
    type:"post",
    url:"billing_table",
    dataType: "html",
    data:{
      i_id_post : send_data, //This is the correct format for your data to send
      b_barcodestring : b_barcodestring
    },
    success:function(data){

      $("#bill_tbl").append(data);

    }

});

Comments

0

you can change the div to table and try this it will work

 function addrow(){
     $("#mytable").after("<tr><td>1</td><td>1</td><td>1</td></tr>");
 }

working fiddle here

In your case change this div(bill_tbl) to table

5 Comments

i am not coding to add row on click, i want to get row that returned from ajx
just use the returned HTML string inside the function as a parameter if you data in success is a valid HTML string it will append just make sure its an table
what is your JSON string here ?
i am just asking...can it be done...if yes then please, prompt me with the hint
if you have any valid string like "<tr><td>1</td><td>1</td></tr>" you can pass it inside the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.