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
https://forums.phpfreaks.com/topic/135419-validating-checkboxes/
Share on other sites

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.