Jump to content

Validating checkboxes


rn14

Recommended Posts

Hi,

 

I have an application where the user selects a number of people that they want to buy the product for.

Based on this number their is a checkbox next to each product.

The customer is then presented with 7 products of which they must select 5.

I have written some simple if statements so that when the user selects the number of people this limits the the number of check boxes to the amount of people they have selected.

To be clear, that means i have seven products on a page and if there are 3 people buying the products that will mean a total of 21 checkboxes. 7 for each person. Each person must select 5.

My problem is trying to validate this to limit each person to picking 5 products:

Here is what i have tryed:

 

<?php
session_start();
$num_of_people = $_SESSION['numofpeople'];
//meal for 1
if($num_of_people == 1) {
if (count($_POST['meal']) == 5) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
}
//meal for 2
}elseif ($num_of_people == 2) {
if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
}
//meal for 3
}elseif ($num_of_people == 3) {
if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
}
//meal for 4
}elseif ($num_of_people == 4) {
if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5 || count($_POST['meal4']) == 5) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
}

//meal for 5
}elseif ($num_of_people == 5) {
if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5 || count($_POST['meal4']) == 5 || count($_POST['meal5']) == 5) {
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
}
}else 
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustest.php">';

?>

 

What happens above is that when a first person buying selects five products the validation does not work.

 

Not sure if i explained this clearly.

 

Thanks for any help

 

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.