0

I want to create table and dynamically add rows when user click button. For the same table column I want to get data from database and display it in drop down list. I used ajax function to get data. Ajax function also work fine but I am unable to add new row to table.Please help me to solve this.

Here is my code; I feel problem with the way I added PHP code in JavaScript function but I don't know how to solve it. Please help me.

html ............................................

<table class="table table-bordered table-hover table-striped">
                                    <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Group</th>                                
                                        <th>Quantity</th>                                
                                        <th>Status</th>
                                        <th>Cost</th>
                                        <th><input type="button" class="addrow btn btn-success" value="Add Row "></th>
                                    </tr>
                                    </thead>
                                    <tbody id="body">
                                    <tr>
                                        <th>1</th>
                                        <td>
                                            <?php $Treatments['#'] = 'Please Select'; ?>
                                            <?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>

                                        </td>

                                        <td><input type="text" class="form-control quantity" size="3" id="quantity" name="quantity[]" size="3" /></td>

                                        <td>
                                            <select class="form-control status" id="status[]" name="status[]">
                                                <option>Proposed</option>
                                                <option>Done</option>
                                                <option>Not Done</option>
                                                <option>Not Required</option>
                                            </select>
                                        </td>

 <td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>
</tr>

 </tbody>
</table>

javascript .................................................

$(function(){
        $('.addrow').click(function(){
            addrow();
        });
 function addrow()
        {
            var row =($('#body tr').length-0)+1;
            var tr ='<tr>'+
                '<th>'+row+'</th>'+
                '<td>'+
                     '<?php $Treatments['#'] = 'Please Select'; ?>'+
                     '<?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>'+

                '</td>'+                
                '<td><input type="text" class="form-control quantity" id="quantity" name="quantity[]" size="3"></td>'+

                '<td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>'+
                '</tr>';
            $('#body').append(tr);
        }
    });     

1 Answer 1

1

try using this,

$('#body tr:last').after(tr);

Fiddle link: http://jsfiddle.net/bMyGY/1137/

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

Comments

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.