reyes99 Posted May 14, 2012 Share Posted May 14, 2012 Hi all, I have been trying to figure this out for a few days already and can't seem to figure it out. I want to display a few images with a checkbox next to it so I can delete multiple images at once when I press the delete button. My problem is that I can't seem to figure out how to check if the checkbox is checked. if I uncheck one of them and click the delete button it deletes one from the array but I don't know which one was unchecked. For example if I uncheck the second one the array shows: Array ( [0] => on [1] => on ) but I don't know which of the three was unchecked? Here is the code I have been testing with: <?php // if submit button is clicked if(isset($_POST['submit'])) { // do delete function(); } // display deletethis[] array if (isset($_POST['deletethis'])) { echo "<pre>"; print_r ($_POST['deletethis']); echo "</pre>"; } $separate = array("http://funnypicturesimages.com/images/image/funny-dog-pictures.jpg", "http://1.bp.blogspot.com/-35wQMpYtNZc/TXWNx8y2xCI/AAAAAAAB_2o/9vZYNfWrGn8/s400/funny_demotivational_posters_01.jpg", "http://3.bp.blogspot.com/-TFnzZ8zFtgg/TXWNpodBkGI/AAAAAAAB_2Q/O_fOOSqFM6w/s400/funny_demotivational_posters_04.jpg"); echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'; // display each image from array foreach ($separate as $value) { echo "<img src=".$value.">"; if ($_POST['deletethis'] = "on") { $checked="checked"; } else { $checked=""; } if ($_POST['deletethis'] != "") { echo "checked"; } else{ echo "unchecked"; } // if checkbox currently checked display it checked else display unchecked if ($checked == "checked") { echo '<input type="checkbox" name="deletethis[]" checked="checked"/><br /><br />'; } else { echo '<input type="checkbox" name="deletethis[]"/><br /><br />'; } } ?> <center><input type="submit" name="submit" value="Delete Checked"></center> </form> Quote Link to comment https://forums.phpfreaks.com/topic/262530-checking-if-checkbox-is-checked-in-array/ Share on other sites More sharing options...
xyph Posted May 14, 2012 Share Posted May 14, 2012 foreach( $array as $key => $value ) use $key to get the value's key Quote Link to comment https://forums.phpfreaks.com/topic/262530-checking-if-checkbox-is-checked-in-array/#findComment-1345458 Share on other sites More sharing options...
reyes99 Posted May 14, 2012 Author Share Posted May 14, 2012 Thank you for your quick response but I tested it with the $key and when I check any of the checkboxes and then click the Delete Button I always get : Array ( [0] => on [1] => on ) for example if I delete the second one I should get Array ( [0] => on [2] => on ) Right? but I am getting 0 & 1 Thanks again. Ralph Quote Link to comment https://forums.phpfreaks.com/topic/262530-checking-if-checkbox-is-checked-in-array/#findComment-1345471 Share on other sites More sharing options...
xyph Posted May 14, 2012 Share Posted May 14, 2012 What? You should use the preview button. You need to provide your form with the image's ID or file name. <input type="checkbox" name="delete[*imageID*]" value="1"> Quote Link to comment https://forums.phpfreaks.com/topic/262530-checking-if-checkbox-is-checked-in-array/#findComment-1345472 Share on other sites More sharing options...
reyes99 Posted May 14, 2012 Author Share Posted May 14, 2012 Sorry, I am kind of a newbie but I figured it out now with your help. Thanks you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/262530-checking-if-checkbox-is-checked-in-array/#findComment-1345475 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.