Take the 2-minute tour ×

At the moment i have this piece of javascript code:

//Display custom confirm box and delete multiple message

$(document).ready(function () {
    $(".delete_button-multiple").click(function () {
        //Get message id as variable
        var dataString = 'id=' + id;
        var parent = $(this).parent();
        //Display custom Confirm box  
        jConfirm('Are you sure you want to delete this message?', '', function (r) {
            if (r == true) { //initiate delete message if agreed

                    type: "POST",
                    url: "delete-mail_ajax.php",
                    data: dataString,
                    cache: false,

                    success: function () {
                        window.location = "mail_inbox.php";

                return false;



$id = mysql_escape_String($id);
$sql = "delete FROM mail WHERE mail_id='$id'";
mysql_query( $sql);

This is a working code for deleting only one mail item. I wrote the following code to delete multiple messages from checkboxes:

        //Multiple delete mail
        $list_mail = $_POST['message'];
        foreach ($list_mail as $messageID){    
        $sql = "delete FROM mail WHERE mail_id='$messageID'";
        //deletion complete, refresh the page
        header("Location: mail_inbox.php");
        }//end delete multiple 

The difficulty i'm having is changing the working code above to incorporate the multiple selection, and deletion, of selected mails.

Any help on this issue would be appreciated


share|improve this question
Where exactly is the problem? I didn't really got that... Selection? I would suggest to use checkboxes. –  Anpher Aug 14 '10 at 20:23
I am using checkboxes. the issue was that i wasn't sure how to pass the array through the jquery function and then into ajax. –  Callum Johnson Aug 14 '10 at 23:05

2 Answers 2

up vote 2 down vote accepted

Assuming you're using checkboxes, your code would look something like:

var messages = new Array();
$("input[name='mail_items[]']:checked").each(function() {

   type: "POST",
   url: "delete-mail_ajax.php",
   data: { message: messages }  //jQuery should translate this to an array that PHP should understand
   cache: false,

You may need to json_decode the input to the $_POST['message'] variable, but I'd do a var_dump() on the stuff first just to make sure what PHP is doing in the background. Can't check at the moment, sorry.

share|improve this answer
thank you for this. i understand everything apart from the var_dump() and json_decode part? –  Callum Johnson Aug 14 '10 at 23:05
That's on the PHP side. Just as a check, do a var_dump($_POST['message']). If you see it as an array, you're in business. If you see it as a string, you'll have to throw it through the json_decode function to make it an array. –  villecoder Aug 15 '10 at 21:19
thanks, i'll be sure to check this out. –  Callum Johnson Aug 16 '10 at 11:50

I guess you have trouble submitting the form via Ajax? There is a neat plugin that does that for you: http://jquery.malsup.com/form/

share|improve this answer
the problem i'm having is, trying to pass the array to the javascript and then to ajax –  Callum Johnson Aug 14 '10 at 20:37

Your Answer


