0

I have a pm system and I would like for all checked messages to be deleted. So far, it only deletes one at a time and never the one selected. Instead it deletes the one with the youngest id value. I'm new to ajax and all help is appreciated.

Here's my function:

 function deletePm(pmid,wrapperid,originator){
    var conf = confirm(originator+"Press OK to confirm deletion of this message and its replies");
    if(conf != true){
        return false;
    }
    var ajax = ajaxObj("POST", "php_parsers/pm_system.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == "delete_ok"){
                _(wrapperid).style.display = 'none';
            } else {
                alert(ajax.responseText);
            }
        }
    }
    ajax.send("action=delete_pm&pmid="+pmid+"&originator="+originator);
}

1 Answer 1

1

You may need to modify your form in order to do this. You have to pass the checkboxes to your PHP script as an array through ajax.

<input type='checkbox' name='pm[]' value='1'>1<br>
<input type='checkbox' name='pm[]' value='2'>2<br>
<input type='checkbox' name='pm[]' value='3'>3<br>

With the checkboxes like this, PHP can handle an array as such:

$_POST['pm'];

You will need to modify your ajax script to be able to send the array, and probably change your PHP script to loop thru the array value it receives. It's probably expecting an integer (a single ID) and you are about to send it an array.

Revised Ajax Method:

$("#submit").on('click',function(e) {

  e.preventDefault();

  var data = {
   'pmIds': $("input[name='pm[]']").serializeArray(),
   'action' : 'delete_pm',
   'originator' : 'whatever'
  };

  $.ajax({
    type: "POST",
    url: 'php_parsers/pm_system.php',
    data: data,
    success: function(result) {
         window.console.log('Successful');
      },
  });
})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Robert, for your reply! Is this method good for an unlimited quality of messages? The pm system will have many users. Sorry if my question was dumb, I'm still learning. :) EDIT: I have the messaged assigned with an ID. Maybe I could add the id into the value field.
I've revised my answer to include the javascript that you could implement to handle your multiple checkboxes and send to your PHP. To answer your comment above, PHP has a 'post_max_size' directive in the ini file. As long as your total POST data size does not exceed that, you should be fine. If you're looping thru each ID when you receive it and deleting them individually in your PHP you may run into Timeout issues though if it's a really large amount of users. Your php script may need to be edited to chunk those deletes.
And yes I would put the ID of the user in the value field of input, if that's the key you're using to delete it in your PHP application. That would be simplest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.