rn14 Posted November 17, 2008 Share Posted November 17, 2008 Hi, Below I output 3 pieces of information from a database in a loop. This is done for 7 records in the database. With each 3 pieces of information there is also a checkbox. The value of this checkbox is set to a value from the database. I want to be able to insert php validation to ensure that the user selects five and only five of these checkboxes. I have a feeling there is a simple solution to this if anybody can point me towards it that would be great. Thanks $result = mysql_query("SELECT * FROM menus_show ORDER BY date DESC LIMIT 0,7 ;"); while($row = mysql_fetch_array($result)) { $image = $row['image']; $title = $row['title']; $description = $row['text']; ?> <div class = "dishes"> <span> <? echo strip_tags($image, '<img>, <span>'); ?> </span> <h2> <?echo strip_tags($title, '<span>');?> </h2> <p> <?echo strip_tags($description, '<span>');?> </p> <p><input type="checkbox" name="meal[]" value="<?php echo $row['id']; ?>" CHECKED/> Include this meal in my selection</p> <div class="clear"></div> <input type="hidden" name="number" value="3"> </div> <? } ?> <input type="submit" value="Submit Changes" name = "b1" onclick = "function.php"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/ Share on other sites More sharing options...
rn14 Posted November 18, 2008 Author Share Posted November 18, 2008 Anybody got any ideas here? Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/#findComment-692443 Share on other sites More sharing options...
.josh Posted November 18, 2008 Share Posted November 18, 2008 You mean like this? if (count($_POST['meal']) > 5)) { // too many } Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/#findComment-692449 Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 You mean like this? if (count($_POST['meal']) > 5)) { // too many } Wouldn't it be this? if (count($_POST['meal']) == 5)) { //just enough }else{ //too many or not enough } Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/#findComment-692453 Share on other sites More sharing options...
rn14 Posted November 18, 2008 Author Share Posted November 18, 2008 thanks for the help guys. Just one more question about this. I want it to prevent the user from going to the next page. How do I adapt this so that when the user hits the form submit button they will remain on this page if they have selected more than 5?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/#findComment-692766 Share on other sites More sharing options...
.josh Posted November 18, 2008 Share Posted November 18, 2008 @ProjectFear: yeah probably. For some reason I interpreted the OP to mean "You can pick up to 5 but no more." @rn14: in the else {..} use a header to redirect back to the form. Quote Link to comment https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/#findComment-692813 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.