Post image for Delete Tabular Data using PHP/Jquery with confirmation dialog box

Delete Tabular Data using PHP/Jquery with confirmation dialog box

by Hyder on May 23, 2010

in Jquery, PHP

Almost 2 months ago,  I wrote an article about how to Remove data in a div using jQuery . 2 of my readers asked me how could you delete the data via ajax if it is in a html table . This is what this article is all about.  I have also added a confirmation dialog box using jQueryui Modal Confirmation .The modal box prompts the user if he or she wants to really delete the data or not.  This is useful to prevent accident deletion of your data.

You can check out an online Demo here :

Online Demo || Download Source Code

I have fully commented the code below ,i think it should not be that difficult to understand .


/*Hiding the div with ID dialog-message and #confirmation_dialog
 These 2 divs are responsible for the modal boxes .
 */

$("#dialog-message").hide();
$("#confirmation_dialog").hide();

/*a Function to Initialise a Dialog instance for the modal box */
function modal_message()
{

 $("#dialog-message").dialog({
 modal: true,
 buttons: {
 Ok: function() {
 $(this).dialog('destroy');

 }
 }
 });
}

$('a.delete').click(function(e){ // if a user clicks on the "delete" image
e.preventDefault(); //prevent the default browser behavior when clicking
var row_id =     $(this).attr('id');
var parent =   $(this).parent().parent();

 $('#confirmation_dialog').dialog({

/*Initialising a confirmation dialog box (with  cancel/OK button)*/

 autoOpen: false,
 width: 600,
 buttons: {
 "Ok": function() { //If the user choose to click on "OK" Button

 $(this).dialog('close'); // Close the Confirmation Box

 $.ajax({//make the Ajax Request
 type: 'get',
 url: 'ajax_delete.php',
 data: 'delete=' + row_id,
 beforeSend: function() {
 parent.animate({'backgroundColor':'yellow'},600);
 },
 success: function(response) {//if the page ajax_delete.php returns the value "1"
 if(response=='1'){
 parent.slideUp(600,function() {//remove the Table row .
 parent.remove();
 });

 modal_message();//Display the success message in the modal box
 }else {
 alert('We could not delete it !');//if ajax_delete.php does not return the value "1"
 }

 }
 });
 },
 "Cancel": function() { //if the User Clicks the button "cancel"
 $(this).dialog('close');
 }
 }
 });

 $('#confirmation_dialog').dialog('open');

//Display confirmation Dialog when user clicks on "delete Image"

 return false;

 });
});

Credits and Summary :

For the CSS  Styled Table  , i am using the veerle’s code .You would probably be deleting data from a Database ,so I highly recommend you to check out my previous article with a fully functional deleting system (including the database part ) .You can also change the look of the modal dialog by referring to this article ( Check out the Theming Tab) .Feel free to drop me a comment  below . Any suggestion  for improvement are welcome.

Related Posts

  1. Remove data in a div after successfully deleted using Jquery
After Post

{ 15 comments… read them below or add one }

Codeforest May 23, 2010 at 9:55 pm

Nice tutorial, but there is too many flashes and effects. I personally like it simple and not so I have to click many times after doing some action. Successful message should not be modal dialog, IMHO.

Reply

Hyder May 23, 2010 at 10:14 pm

Thanks Zvonko , I agree with you about success message in dialog box :) may be making the modal box to disappear after a few seconds with a fade out effect ( similar to facebook ) would be more practical .

Reply

Adam August 2, 2010 at 6:32 am

Hi…
I a beginner programmer… Just wanna ask, how you return the value 1 from the ajax_delete.php?
I stuck with that right now…
Thank for the tutorial…

Reply

Hyder August 2, 2010 at 4:35 pm

@Adam : Hello , Actually in the ajax_delete.php , you should write code to delete the row id from the database . It should have been like this :

	$Id_to_delete = $_GET['id'] ;
    $query = 'DELETE from table_name WHERE id ='.$_GET['id'];
    $result = mysqli_query($dbc,$query);//Run the Query
    if($result){ // If deleted successfully , return the value 1
        echo '1';//
    }

Actually the value 1 , you should echo it only if the row has been deleted . If you do not echo the value 1 , then jQuery will not remove the row .

Check line 47-50 above for the jQuery Code :

 if(response=='1'){//the 1 comes from ajax_delete.php
 parent.slideUp(600,function() {//remove the Table row .
 parent.remove();
 });

Let me know if you need more clarification Adam :)

Reply

Pieter v.d. Veen August 7, 2012 at 6:55 pm

best Hyder,

I tryed in ajax_delete.php the following lines, but is dont work.

include ‘../connectdb/connectdb.php’;
include ‘../error/errordb.php’;

$code = $_GET['id'] ;
$query = “DELETE FROM website WHERE websitecode = $code”;
$resultaat = mysql_query($query) or die (error($query, $_SERVER['PHP_SELF'])); //Run the Query
if($resultaat){ // If deleted successfully , return the value 1
echo ‘1′;//
}

Reply

Mike Howey September 9, 2010 at 8:14 pm

I love this. I’m still trying to figure out the whole jquery this. My question is can this be done will lists instead of tables? removing from a . Something like

test 1
test 1
test 1

Reply

Mike Howey September 9, 2010 at 11:52 pm

Rather then this var
row_id = $(this).attr(‘id’);

is there a way to attach a var?

I love this script, the best one I’ve found. I’m a little lost. I can get it to remove 1 row. After that the messages still pops up but it doesn’t remove the row. I can’t figure out where to get the id from.

Reply

Senaka July 21, 2011 at 8:04 am

Excellent tutorial .

Reply

Dayo July 29, 2011 at 10:57 am

hello hyder… am a beginner programmer… how do i make d delete image appear in only one status instead of all?

Reply

Hyder July 29, 2011 at 12:29 pm

@Dayo : for which reason would you want to place only one delete image? where do you want to place the cross image? and what action should occur on clicking on it ? these info will allow me to help you much easier :)

Reply

agent_virus April 23, 2012 at 7:10 am

nice……..thanks

Reply

Hekmat July 21, 2012 at 12:05 am

Hi
pls Help me.I want make a confirm box with jquery when I press OK delete some rows from my database
I search many thing about this bout when i used it had some problem .Tnx

Reply

MhdAlwan August 29, 2012 at 8:14 pm

Hi hydar

i’ve done what you say but he tell me that the variable is undefined
what i have to do

Reply

manoj September 28, 2012 at 5:30 am

really its nice code Can i Use it for my Personal website

Reply

Hyder September 28, 2012 at 7:12 am

Yes ,sure :)

Reply

Leave a Comment

Previous post:

Next post: