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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.