jeudi 12 février 2015

Add a element to dynamic table, dynamically without a page refresh, php jquery


I am trying to add a dynamic row to the existing table on click of button, whose rows are dynamically created using output from PHP script.


I doing an ajax call to the script insert_tr.php which returns me a TR element in the same format as the existing table with the data.Data is returned appropriately


But unfortunately, the <tr> row is not being added to the table dynamically but adds only after a page refresh.


PHP file code :



<div id="v_div">
<table class="table table-bordered table-striped" >
<thead>
<th class='col-md-2'>name</th>
<th class='col-md-2'>number</th>
</thead>

<tbody>
<?php
while ($data = pg_fetch_assoc($ret)) {

echo
"<tr id=tr_".$data['sr_number'].">
<td class='td_topic' id=title:".$data['number']." >".trim($data['name'])."</td>
<td class='td_topic' id=title:".$data['number']." >".trim($data['number'])."</td>
<td class='td_topic' id=title:".$data['number']." ><button class='btn btn-info check1' type=button title='Add Entry'>Add Entry</button></td>
</tr>";
?>
</tbody>
</table>
</div>


Javascript :



$(document).ready(function() {
$("#v_div").on('click', '.check1', function() {
var field_userid = $(this).parent().attr("id");
var value = $(this).text();
$.post('insert_tr.php', field_userid + "=" + value, function(data) {
if (data != '') {
$(this).closest("tr").after(data);

}
});

});
});


All I want to do is add the row immediately after the current TR am on ,dynamically without a page refresh, which serves the ultimate use of an ajax call.





Aucun commentaire:

Enregistrer un commentaire