I am using the dataTables jQuery plugin to display a table in a wordpress site with data from a custom table and it works well.
I have added select check boxes and once a row or rows have been selected and the Delete button is clicked the form calls an external non-wordpress file to delete the rows corresponding to the IDs in the GET (for me to see the data being passed).
I know dataTables can do this for me instead but I am not good enough at JS to implement it can anyone help?
Also I know I should be using the correct Wordpress $wpdb delete but just found this easier as I am used to doing things outside of wordpress.
Here are the pertinent parts of the files I am using.
<form action="<?php echo plugins_url('process/process-form-delete.php',__FILE__) ; ?>" method="get">
<table id ="my-parts">
<thead>
<tr>
<th>Select</th>
<th>Part Image</th>
<th>Part Name</th>
<th>Vehicle</th>
<th>Seller Type</th>
<th>Location</th>
<th>Price</th>
<th>Date</th>
<th>View</th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
$part_details = pl_get_part_details($row->pl_part_ID);
?>
<tr id="<?php echo $row->my_part_ID; ?>">
<td><input name="selector[]" type="checkbox" value="<?php echo $row->my_part_ID; ?>" /></td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
<td> Stuff Here </td>
</tr>
<?php
} ?>
</tbody>
</table>
<button class="btn btn-primary" name="submit_mult" type="submit">Delete</button>
</form>
Process-form-delete.php
<?php
require_once ('../config/dbwpconfig.php');
$number = $_GET['my-parts_length'];
$i=0;
while ($i < $number) {
$data = $_GET['selector'];
if (isset($data[$i])) {
$sql = "DELETE FROM wp_pl_my_parts WHERE my_part_ID = :partID";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':partID', $data[$i], PDO::PARAM_INT);
$stmt->execute();
}
$i++;
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
jQuery to get table to become a datatables table
jQuery(document).ready( function() {
jQuery('#my-parts').dataTable( {
"aaSorting": [[2,'asc']],
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0,8 ] }
]
} );
} );