Hello wise stackoverflow users
I am currently working on a private messaging system in PHP, and i am stuck at making a function to delete multiple selected messages. As it is right now, it is working but i get the warning:
Warning: Invalid argument supplied for foreach() in /var/www/PM/delete.php on line 7
I use jquery to sent the data to another php file called delete.php.
First thing first, my checkboxes look like this:
<input type="checkbox" class="message_checkbox" name="pms[]" value="<? echo $loadData['id']; ?>" id="<? echo $loadData['id']; ?>">
And my jquery script look like the following:
<script>
$(function(){
$("a.delete").click(function(){
var message = new Array();
$("input[@name='pms[]']:checked").each(function() {
message.push($(this).val());
});
$.ajax({
type: 'POST',
url: 'PM/delete.php',
data: { id: message },
success: function(html) {
alert("all done");
}
});
})
})
</script>
My delete.php looks like the following:
<?php ob_start();
include("../config.php");
$rows2del = $_POST["id"];
foreach($rows2del as $id) /* Line 7*/
{
mysql_query("UPDATE user_pm SET reciev_deleted = '1' WHERE id = '$id'") or die(mysql_error());
}
?>
Anyone who can tell me what i am doing wrong?
Note: Im very new in the jquery and javascript field!
Your selector is wrong
$("input[@name='pms[]']:checked")there is no@nameattribute you might be thinking about xpath. It should just be$("input[name='pms[]']:checked")So you’re passing an empty array here
data: { id: message },which will not add theidparameter to you post request, therefore$_POST["id"];will be empty.