Reputation: 4323
I have created a form to select category items from main category. Here I use checkboxes to select category item. In my form there are many main categories and have its own subcategories. Example Courses and its subjects.
With this form I need to check how many sub items have selected for a one category. And also I need to look over at least one sub category has selected in all main category and if it is ok want to limit upto 10 sub categories to select.
I tried it something like this.. but here its count all selected subcategories.
if ( isset ( $_POST['sub-category']) && is_array( $_POST['sub-category'])) {
$totalCategory = count( $_POST['sub-category']);
if ( $totalCategory >= 1 && $totalCategory <= 10 ) {
$_SESSION['sub-category'] = $_POST['sub-category'];
$url = BASE_URI . 'register.php'; // Define the URL:
header("Location: $url");
exit(); // Quit the script.
} else {
echo 'ERROR: Please select atleast 1, not more than 10.';
}
} else {
echo 'ERROR: Please select atleast one category.';
}
UPDATE:
This is PHP I use to generate from
echo "<input type='checkbox' value='{catId:subID}' name='subject{$lastCatId}[]' />{$lastCatName}";
Them HTML generate something like this..
<h3>Main Category 01
<input type='checkbox' value='3:4' name='subject3[]' />sub category
<input type='checkbox' value='3:6' name='subject3[]' />sub category
<h3>Main Category 02
<input type='checkbox' value='4:6' name='subject4[]' />sub category
<input type='checkbox' value='4:2' name='subject4[]' />sub category
<h3>Main Category 01
<input type='checkbox' value='5:8' name='subject5[]' />sub category
<input type='checkbox' value='5:4' name='subject5[]' />sub category
Problem is how I get sub categories' values separately and then need to count...
I hope someone help me out... Thank you.
Upvotes: 0
Views: 115
Reputation: 4912
Try this:
<h3>Main Category 01
<input type='checkbox' value='sub_cat_1' name='main_cat_1_subject[]' />sub category1
<input type='checkbox' value='sub_cat_2' name='main_cat_1_subject[]' />sub category2
<input type='checkbox' value='sub_cat_3' name='main_cat_1_subject[]' />sub category3
<input type='checkbox' value='sub_cat_4' name='main_cat_1_subject[]' />sub category4
<h3>Main Category 02
<input type='checkbox' value='sub_cat_1' name='main_cat_2_subject[]' />sub category1
<input type='checkbox' value='sub_cat_2' name='main_cat_2_subject[]' />sub category2
<input type='checkbox' value='sub_cat_3' name='main_cat_2_subject[]' />sub category3
<input type='checkbox' value='sub_cat_4' name='main_cat_2_subject[]' />sub category4
<h3>Main Category 01
<input type='checkbox' value='sub_cat_1' name='main_cat_3_subject[]' />sub category1
<input type='checkbox' value='sub_cat_2' name='main_cat_3_subject[]' />sub category2
<input type='checkbox' value='sub_cat_3' name='main_cat_3_subject[]' />sub category3
<input type='checkbox' value='sub_cat_4' name='main_cat_3_subject[]' />sub category4
And the php:
if(isset($_POST['main_cat_1_subject']) && !empty($_POST['main_cat_1_subject'])){
$main_cat_subjects = $_POST['main_cat_1_subject'];
//do the stuff.
foreach($main_cat_subjects as $index => $main_cat_subject){
//Sub category items by index here.
}
} else {
echo 'Please select atleast on subject for Main Category 1';
//redirect or die
}
Repeat this loop for all the categories and change the names in the $_POST
array.
Upvotes: 1
Reputation: 32740
Change your html code to
<h3>Main Category 01
<input type='checkbox' value='' name='sub_cat1[]' />sub category
<input type='checkbox' value='' name='sub_cat1[]' />sub category
<input type='checkbox' value='' name='sub_cat1[]' />sub category
<input type='checkbox' value='' name='sub_cat1[]' />sub category
<h3>Main Category 02
<input type='checkbox' value='' name='sub_cat2[]' />sub category
<input type='checkbox' value='' name='sub_cat2[]' />sub category
<input type='checkbox' value='' name='sub_cat2[]' />sub category
<input type='checkbox' value='' name='sub_cat2[]' />sub category
<h3>Main Category 01
<input type='checkbox' value='' name='sub_cat3[]' />sub category
<input type='checkbox' value='' name='sub_cat3[]' />sub category
<input type='checkbox' value='' name='sub_cat3[]' />sub category
<input type='checkbox' value='' name='sub_cat3[]' />sub category
And submit the form, which will give you the out put
$count_cat1 = count($_POST['sub_cat1']);
$count_cat2 = count($_POST['sub_cat2']);
$count_cat3 = count($_POST['sub_cat3']);
You can put it in a loop to reduce the code length.
Upvotes: 1
Reputation: 3534
When naming your check boxes, use this format:
<input type="checkbox" name="subject[math][algebra]">
<input type="checkbox" name="subject[math][calculus]">
in PHP:
<?php
$subjects = $_POST['subject'];
$math = $subjects['math'];
$algebra = $math['algebra'];
...
That is the basic idea.
Upvotes: 1