liam1412 Posted February 1, 2007 Share Posted February 1, 2007 Anyone know where I can find out how to do this or give me a rough idea that I can work with Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/ Share on other sites More sharing options...
realjumper Posted February 1, 2007 Share Posted February 1, 2007 Something like this should work <form name="my_form" method="POST" action="action.php"> <table align="center" border="0"> <?php $query = mysql_query("SELECT * FROM forumdb"); while($r = mysql_fetch_array($query)){ echo "<tr><td>"; echo $r['posts']." </td><td><input type='checkbox' name='select[]' value='".$r['id']."'></td></tr>"; //just print a title and a text box. note the [], this creates an array of all selected checkboxes. } ?> <tr> <td><input type="submit" name="submit" value="Delete Selected Posts"></td> </tr> </table> and on your 'action.php' page.... <?php $selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc... if($selected == ''){ echo "Nothing selected!"; }else{ mysql_query("DELETE FROM forumdb WHERE id in ($selected)"); //deletes all from table if id is in variable echo "Deleted posts!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174885 Share on other sites More sharing options...
liam1412 Posted February 1, 2007 Author Share Posted February 1, 2007 Thats mint. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174911 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 <input type='checkbox' name='select[]' I've worked with html for > 10 years, and this taught me something new :-) I've named other form elements with array notation before (select multiples etc.), but until now I've created serialized strings of dynamically created checkbox names and sent the string data with a hidden box :-) Imagined that I tried this a long time ago and found that it wasn't possible with checkboxes, but obviously I either tried it a bit too early, or i did something wrong :-) Just shows why programming/markup is so fun; you learn something new every day, even when you don't expect it :-) Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174938 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 okay then you might also enjoy this bit of advice: if you have a list of checkboxes and you do that, it will post the ones you checked, but the array will always start at 0 and count from there, so even if you check the 1st, 5th, 6th, and 8th checkbox out of 10 (for example), the array will be posted as $select[0]-$select[3]. The point is, you don't know which checkboxes were checked. To get around that, you can explicitly assign the array position in the form, like name = 'select[0]', name = 'select[1]' etc... that way it will post the $select array, but the info will be posted in the specific element positions, so you can know which ones were checked. Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174949 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 okay then you might also enjoy this bit of advice: if you have a list of checkboxes and you do that, it will post the ones you checked, but the array will always start at 0 and count from there, so even if you check the 1st, 5th, 6th, and 8th checkbox out of 10 (for example), the array will be posted as $select[0]-$select[3]. The point is, you don't know which checkboxes were checked. To get around that, you can explicitly assign the array position in the form, like name = 'select[0]', name = 'select[1]' etc... that way it will post the $select array, but the info will be posted in the specific element positions, so you can know which ones were checked. Well, thats evidently :-) But when you supply the value for the checkbox like in the example above, you got all that you need. The array order is not interesting, only the values inside it. Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174970 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 you mean you can't think of a single reason that that wouldn't be useful? Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-174972 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 you mean you can't think of a single reason that that wouldn't be useful? Sorry crayon, my reply was unclear and might lead to this misunderstanding. Yes, I can think of several ways it can be helpful, a "hellofalot" actually. In most cases i would too say that this is a better solution than just using []. I Just ment to say that in the context of this thread, the order of the array was not interesting. I had actually tested numbered indexing [\d] 30 seconds after i read about [] in this thread :-) In a database-listing context, the possibility of using row id's as array indexes for the checkbox array creates lots of opportunities. A checkbox object now has posting now has 3 interesting variables; name, value, unchecked. I see new possibilites in both posting and js event handling. Anyways, I do appreachiate you trying to give me some good advice :-) Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-175029 Share on other sites More sharing options...
.josh Posted February 1, 2007 Share Posted February 1, 2007 ic. I re-read your post and I suppose it could have indeed been just as easily taken in that context. cheers! Quote Link to comment https://forums.phpfreaks.com/topic/36672-deleting-several-posts-by-the-use-of-checkboxes/#findComment-175034 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.