Jump to content

Checkbox Validation in a Loop


rn14

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/133124-checkbox-validation-in-a-loop/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.