AJLX Posted February 23, 2011 Share Posted February 23, 2011 Hello All, Hopefully this should be an easy one. I have the following select box: echo '<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value='.$row['ID']."></td>"; I get these variables on a separate page and try and run them through a loop so that I can delete all of the select items. It is this bit that I'm struggling with at the moment. Here is what I have so far: <?php session_start(); include("inc/session.inc.php"); include("inc/conf2.inc.php"); foreach($_POST['checkbox'] as $value); $sql_query = mysql_query("DELETE FROM messages WHERE ID = '$value'"); ?> At the moment this doesn't appear to be working. Does anyone have any ideas? Regards AJLX Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/ Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 What about it isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178929 Share on other sites More sharing options...
sikken Posted February 23, 2011 Share Posted February 23, 2011 this might help HTML: <input type="checkbox" name="stuff" value="a" checked="checked" /> <input type="checkbox" name="stuff" value="b" checked="checked" /> <input type="checkbox" name="stuff" value="c" checked="checked" /> PHP: foreach($_GET['stuff'] as $val) echo 'val='.$val; RESULT: val=a val=b val=c Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178932 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 That code would return an "Invalid argument supplied for foreach()" warning. Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178940 Share on other sites More sharing options...
AJLX Posted February 23, 2011 Author Share Posted February 23, 2011 It is worth noting that the check box code sits in a 'while' statement so that there are lots of them, each of which have the message ID attached to them. I want to then delete messages by this ID. Regards AJLX Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178941 Share on other sites More sharing options...
ChemicalBliss Posted February 24, 2011 Share Posted February 24, 2011 Try this and see what it says: <?php session_start(); include("inc/session.inc.php"); include("inc/conf2.inc.php"); print_r($_POST); echo("\n\n<br />"); foreach($_POST['checkbox'] as $value){ $sql_query = "DELETE FROM messages WHERE ID = '$value'"; $sql_result = mysql_query($sql_query); } ?> use "view page source" and copy/paste the results, maybe you will find the answer yourself Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178945 Share on other sites More sharing options...
AJLX Posted February 24, 2011 Author Share Posted February 24, 2011 Hello guys, Using ChemicalBliss's code I get the following returned: Array ( ) Warning: Invalid argument supplied for foreach() in /home/ajlxcou1/public_html/ProjectA/delete_message.php on line 8 My guess would be that the array is empty. Which then means that the foreach statement has nothing to run, and therefor throws me back an error? Is this correct? I'm not asking you guys to write this for me, but a prod in the right direction is useful! Regards, AJLX Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1179265 Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 Correct . Check your form html. Make sure your form tag has the method attribute set to post. Eg. <form method="post"> Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1179353 Share on other sites More sharing options...
AJLX Posted February 25, 2011 Author Share Posted February 25, 2011 Perfect I had a couple of html issues. 1) I had post[] instead of post. In my mind it made sense to be posting an array. 2) I had $rows[iD] instead of $row[iD] All working now. Special thanks to ChemicalBliss. Regards, AJLX Quote Link to comment https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1179784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.