简体   繁体   中英

How to make a JavaScript function that will delete table rows from a MySQL database

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?

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM