Reputation:
I am a new developer and I have a problem. I think you can solve this. I want to show confirm box before delete. How Can I do that this my Ajax code HTML:
<input type='button' class="btn btn-danger delete_button" id="delete_button<?php echo $row->id;?>" value="delete" onclick="delete_row('<?php echo $row->id;?>');">
ajax
function delete_row(id)
{
jQuery.ajax({
type:'post',
url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",
data:{
delete_row:'delete_row',
row_id:id,
},
success:function(response) {
if(response=="success")
{
var row=document.getElementById("row"+id);
row.parentNode.removeChild(row);
}
top.location.href="admin.php?page=data_list";
}
});
}
Wordpress PHP
function delete_form()
{
if(isset($_POST['delete_row']))
{
$id=$_POST['row_id'];
echo $row_no;
global $wpdb;
$table_name = "countries";
$wpdb->delete($table_name,
array( 'id' => $id ) );
exit();
}
}
add_action( 'wp_ajax_delete_form', 'delete_form' );
add_action('wp_ajax_nopriv_delete_form', 'delete_form');
Upvotes: 0
Views: 1580
Reputation: 12355
Have a look at this code I have: https://github.com/delboy1978uk/bs-delete-confirm
If you use Bootstrap in your project (EDIT - I just noticed you are! :-D), just follow the README.md! All it takes is to add a class to the button you want the confirm modal hooked up to. If not, take a look at the actual code, and tweak the HTML to whatever, and edit the javascript call or the popup dialog:
(function($) {
$.fn.extend({
deleteConfirm: function(options)
{
var defaults =
{
heading: "Please confirm",
body: 'Are you sure you wish to perform this action?',
ok_text: 'Proceed',
cancel_text: 'Back',
log: false
};
options = $.extend(defaults,options);
$('body').append('<div class="modal fade" id="bs-delete-confirm" tabindex="-1" role="dialog" aria-labelledby="bs-delete-confirm-label" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h4 class="modal-title" id="bs-delete-confirm-label">Please confirm</h4></div><div id="bs-delete-confirm-body" class="modal-body"></div><div class="modal-footer"><a id="bs-delete-confirm-cancel" class="btn btn-default" data-dismiss="modal" aria-hidden="true"></a><a id="bs-delete-confirm-ok" class="btn btn-primary"></a></div></div></div></div>');
$('#bs-delete-confirm-label').html(options.heading);
$('#bs-delete-confirm-body').html(options.body);
$('#bs-delete-confirm-cancel').html(options.cancel_text);
$('#bs-delete-confirm-ok').html(options.ok_text);
this.each(function() {
$(this).on('click',function(e){
e.preventDefault();
link = $(this).prop('href');
$('#bs-delete-confirm-ok').prop('href',link);
$('#bs-delete-confirm').modal();
if(options.log == true){
console.log('Clicked link = ' + link);
}
});
});
}
});
})(jQuery);
Upvotes: 0
Reputation: 8249
Add this as a first line to your delete_row():
if(confirm('Are you sure you want to delete?')) { // code inside this... }
Upvotes: 1
Reputation: 28513
You can put confirmbox before ajax call, see below code
function delete_row(id)
{
if (confirm("Do you want to delete?") == true) {
jQuery.ajax({
type:'post',
url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",
data:{
delete_row:'delete_row',
row_id:id,
},
success:function(response) {
if(response=="success")
{
var row=document.getElementById("row"+id);
row.parentNode.removeChild(row);
}
top.location.href="admin.php?page=data_list";
}
});
}
}
Upvotes: 3
Reputation: 22490
Try with confirm
Method
function delete_row(id)
{
if(!confirm('Are you sure to delet')){
return false;
}
jQuery.ajax({
type:'post',
url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",
data:{
delete_row:'delete_row',
row_id:id,
},
success:function(response) {
if(response=="success")
{
var row=document.getElementById("row"+id);
row.parentNode.removeChild(row);
}
top.location.href="admin.php?page=data_list";
}
});
}
Upvotes: 1