I am new to AJAX (and website development in general) and I am having trouble deleting a database entry in wordpress using jQuery and AJAX.
The user selects an entry in a table and clicks a button to delete it.
The table is created using datatables plugin and extensive coding to make it work the way I need it to.
the jQuery code is in functions.php and the actual query is in a file on the server.
I have been using this how to delete records from database with an Ajax as a baseline for the code. Also, note that I am using wordpress
My problem is that it does not seem to delete the entry from the database.
Here is the code in functions.php:
var GroupID2Del = dt.row({
selected: true
})
.data()[23];
if (confirm('Are you sure you want to delete this row?')) {
jQuery.ajax({
type: 'POST',
url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
data: 'groupid=' + GroupID2Del,
success: function(data) {
dt.row({
selected: true
})
.remove();
jQuery('#UserTable')
.DataTable()
.draw();
alert(data);
}
});
}
GroupID2Del is how I am getting the id for it the selected row in the table. I am then removing the row from the table itself. The row does get removed.
Here is the ViewTable_DeleteEntry.php
<?php
$dataPosted = $_POST['groupid'];
$sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id =' . $dataPosted);
mysql_query($sql);
$count = mysql_affected_rows();
print $count;
?>
alert in function.php
provides the correct sql, but the $count
is empty since it does not actually delete anything.