UnknownPlayer Posted June 14, 2011 Share Posted June 14, 2011 I have this code: $query = "SELECT * FROM boje"; $result = mysql_query($query, $connection); while ($boja = mysql_fetch_array($result)) { echo '<input type="checkbox" name="boja[]" value="'.$boja['id'].'" />' } Now problem is when i click submit, and when go through this code: foreach ($_POST['boja'] as $boja_id) { if (isset($boja_id)) { echo "isset"; } else { echo "not isset"; } } this works for checkboxes which are checked, but it doesn't show echo message for checkboxes which are not checked(not isset, else code..), what is the problem, and how can i make solution for this? Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/ Share on other sites More sharing options...
joel24 Posted June 15, 2011 Share Posted June 15, 2011 the check-boxes will only post a value if they're selected, so the $_POST['boja'] array will only contain the values of the checked boxes Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/#findComment-1229812 Share on other sites More sharing options...
UnknownPlayer Posted June 15, 2011 Author Share Posted June 15, 2011 And there is no way that he can contain values 0 and 1, i mean in this example ? Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/#findComment-1229815 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 I can't see why you would want to do that. Maybe explain what you're trying to accomplish and I can help better. Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/#findComment-1229816 Share on other sites More sharing options...
cyberRobot Posted June 15, 2011 Share Posted June 15, 2011 Instead of using the foreach to test which checkboxes were checked, what about using the while loop used to create the checkboxes again? You could loop through the database values to see which ones appear in the passed array. Note that I'm not very familiar with using the array syntax for checkboxes, so I'm not sure what the code would be off hand. Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/#findComment-1229833 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Instead of using the foreach to test which checkboxes were checked, what about using the while loop used to create the checkboxes again? You could loop through the database values to see which ones appear in the passed array. Note that I'm not very familiar with using the array syntax for checkboxes, so I'm not sure what the code would be off hand. Something like this should do the trick $query = "SELECT * FROM boje"; $result = mysql_query($query, $connection); $boja_checked = $_POST['boja']; while ($boja = mysql_fetch_array($result)) { $checked = (in_array($boja['id'], $boja_checked)) ? 'checked="checked"' : ''; echo '<input type="checkbox" name="boja[]" value="'.$boja['id'].'" ' . $checked . ' />'; } Quote Link to comment https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/#findComment-1229877 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.