Reputation: 301
As the title says, I was wondering if there is any way to create a function in JavaScript that will delete a row from a table? Maybe by calling some PHP to delete a row in the table?
Upvotes: 1
Views: 1966
Reputation: 152206
For example if you have a table with a few columns, add a new column with some delete me
button:
<tr rel="34">
<td>34</td>
<td>Joe</td>
<td><a class="deleteRow" href="#">delete me</a></td>
</tr>
Then in jQuery
for example add a click event to .deleteRow
class:
$('.deleteRow').click(function(){
var parent = $(this).parent('tr');
var rowId = parent.attr('rel');
$.ajax({
type: 'post',
url: "delete.php",
data: {id:rowId},
success: function(){
parent.remove();
}
});
});
And in delete.php
script:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$id = (int) $_POST['id'];
// make sql query to remove element with given id
}
Upvotes: 3
Reputation:
I think the only way to call a php function from javascript is by making an ajax call. Since javascript is client side and php is server side so there is no other way to do so.
Upvotes: 1