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.
Please Try This:
var myKeyVals = { groupid : GroupID2Del}
jQuery.ajax(
{
type: 'POST',
url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
data: myKeyVals,
success: function(data)
{
dt.row({selected: true }).remove();
jQuery('#UserTable').DataTable().draw();
alert(data);
}
});
AND
$dataPosted = $_POST['groupid'];
if($dataPosted != ''){
$sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id ='.$dataPosted);
mysql_query($sql);
$count = mysql_affected_rows();
} else {
echo 'Blank Record';
}
So the problem was that I am not connected to the database, so i added the following lines to at the beginning of the php file
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
mysql_connect($servername, $username, $password, $dbname) or die("Connection failed");
mysql_select_db($dbname) or die("No Database found.");
?>
function delete_row()
{
$id=$_REQUEST['post_id'];
$t_response = array('status'=>4,'msg'=>'Invalid Parameter');
if( $id == false ) {
$t_response = array('status'=>3,'msg'=>'Invalid ID');
} else {
global $wpdb;
$remove = $wpdb->query("DELETE FROM wp_posts WHERE ID = ".$id);
if($remove) {
$t_response = array('status'=>1,'msg'=>'Record Deleted');
} else {
$t_response = array('status'=>2,'msg'=>'Error Occure While Deleting Your Record Deleted');
}
}
echo json_encode($t_response);
die();
}
Ajax file
$.ajax({
url: custom_js.ajax,
type: 'POST',
dataType: "json",
data: {
'action': 'my_action',
'post_id': id
},
success: function(data) {
if (data.status == 1) {
console.log(data);
s_row.remove();
} else {
alert(data.msg);
}
}
});