How do I show the form based on my checkbox value - php

Based on the user selection I want to show the form elements. Here the user selects class1,class6,engg. How do I bring the all form element in one common form right know I am having different form and form field for class 1,6.
My questions Are:
If user select class1 I have list class1 form?
If User select class2 I have to combine class1 and class6 under one form.
Like wise user select class1,class2,class6,engg.
I want to make all the form elments under one form.
My FORM
<form name="frm" method="post">
<table align="center">
<tr>
<td>Name</td>
<td><input type="text" name="txt" id="txt" required ></td><td></td>
</tr>
<tr>
<td>Product</td>
<td>
<input type="checkbox" name="chk[]" value="class1">class1
<input type="checkbox" name="chk[]" value="class6">class6
<input type="checkbox" name="chk[]" value="class8">class8
<input type="checkbox" name="chk[]" value="class10">class10
<input type="checkbox" name="chk[]" value="class12">class12
<input type="checkbox" name="chk[]" value="engineering">engineering
<input type="checkbox" name="chk[]" value="technology">technology
</td><td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save"></td><td></td>
</tr>
</table>
</form>
My PHP VALUES
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$product = $_POST['chk'];
print_r($product);
foreach($product as $k=> $v)
{
if($v=="class1")
{
?>
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
if($v=="class1" && $v="class6")
{
?>
<form name="f1" action="" method="post">
<p>class1</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<p>class6</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit" name="submit" value="submit">
</form>
<?php
}
}
}
?>
I want output like this:
For class1 form:
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<input type="submit" name="submit" value="submit">
</form>
For class1, class6 form:
<form name="f1" action="" method="post">
<p>class1</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<p>class6</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit" name="submit" value="submit">
</form>

Related

How to separte the value in array_intersect in php

Based on the user selection I want to show the form elements.Here the user selects class1,class6,engg.how do I bring the all form element in one common form right know I am having different form for class 1,6,8 and many more.if user selected the all three classes(1,6,8), class 1 has differnet subject,like wise class 6 is different subject and engg has differen subject.Now I want to show all form elements under one form based one user selections. it user wish.user select class1 means i have to show class1 form. so far I have done. my htmlform,my phpvalues, I have used array_intersect.now many problem is if user picks class(1,6,8) How do I bring all the form element under one form and has one submit button.
<form name="frm" method="post">
<table align="center">
<tr>
<td>Name</td>
<td><input type="text" name="txt" id="txt" required ></td><td></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pass" required></td><td></td>
</tr>
<tr>
<td>Product</td>
<td>
<input type="checkbox" name="chk[]" value="class1">class1
<input type="checkbox" name="chk[]" value="class6">class6
<input type="checkbox" name="chk[]" value="class8">class8
<input type="checkbox" name="chk[]" value="class10">class10
<input type="checkbox" name="chk[]" value="class12">class12
<input type="checkbox" name="chk[]" value="engineering">engineering
<input type="checkbox" name="chk[]" value="technology">technology
</td><td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save"></td><td></td>
</tr>
</table>
My PHP VALUES
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$pass = $_POST['pass'];
$product = $_POST['chk'];
$class1SubjectFormValues = array('class1', 'class6', 'class8', ... );
$class12SubjectFormValues = array('class10', 'class12', 'class8','eng' );
if (array_intersect($product, $class1SubjectFormValues)) {
// class1 subject form
}
if (array_intersect($product, $class12SubjectFormValues)) {
// class12 subject form
}
?>
**Here is my class1 form**
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
**Here is my class6 form**
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
</form>
</p>
**Here is my engg form**
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">csc
<input type="checkbox" name="chk[]" value="science">IT
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
</form>
</p>
I Want output like this:
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<p>class6
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
</p>
<p>class 8</p>
<input type="checkbox" name="chk[]" value="allsubject">csc
<input type="checkbox" name="chk[]" value="science">IT
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit">
</form>

How do I separate the array value in php because my checkbox field contains different values?

I have created the checkbox as an array. Now I want to separate the checkbox values based on the user selection of checkbox and then I want to show the form.
After using foreach function I got the value like this class1,class6,class8.
If it is class1 I have show the form fields.
If it is class6 I have to show the form fields.
How to separate the checkbox and match the with if condition.
My form:
<form name="frm" method="post">
<table align="center">
<tr>
<td>Name</td>
<td><input type="text" name="txt" id="txt" required ></td><td></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pass" required></td><td></td>
</tr>
<tr>
<td>Product</td>
<td>
<input type="checkbox" name="chk[]" value="class1">class1
<input type="checkbox" name="chk[]" value="class6">class6
<input type="checkbox" name="chk[]" value="class8">class8
<input type="checkbox" name="chk[]" value="class10">class10
<input type="checkbox" name="chk[]" value="class12">class12
<input type="checkbox" name="chk[]" value="engineering">engineering
<input type="checkbox" name="chk[]" value="technology">technology
</td><td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save"></td><td></td>
</tr>
</table>
My PHP values:
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$pass = $_POST['pass'];
$product = $_POST['chk'];
//print_r($product);
foreach ($product as $result => $v)
{
echo "The item has" . $v . "<br/>";
}
}
?>
if($v=="class1" || $v=="class6" || $v=="class8" || $v=="class10")
{
?>
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
<?php
}
?>
<?php
if($v=="class10" || $v=="class12" )
{
?>
<p>class12 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="phyiscs">phyiscs
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
<?php
}
?>
Here is my class1 form
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
I hope every one understand my question
It seems you are checking the value of $v outside the foreach loop, which does not make sense -- $v is just one of the possibly many values in the array, so you can't use it to draw conclusions safely.
You could test with in_array instead:
if (in_array("class1", $product) || in_array("class6", $product) || ... ) {
// show some elements
}
Of course that gets cumbersome if you want to check for a lot of possibilities. In that case you could use array_intersect and introduce some variables for readability:
$class1SubjectFormValues = ['class1', 'class6', 'class8', ... ];
$class12SubjectFormValues = ['class10', 'class12', 'class8', ... ];
if (array_intersect($product, $class1SubjectFormValues)) {
// class1 subject form
}
if (array_intersect($product, $class12SubjectFormValues)) {
// class12 subject form
}

I want to show the form elements based on user selection

Based on the user selection I want to show the form elements.Here the user selects class1,class6,engg. How do I bring the all form element in one common form right know I am having different form and form field for class 1,6. I am struggling to solve this issue.I don't know How to fix this.
My questions Are:
If user select class1 I have list class1 form?
If User select class2 I have to combine class1 and class6 under one form.
Like wise user select class1,class2,class6,engg. I want to make all the form elments under one form.
I have given my output sample?
My FORM
<form name="frm" method="post">
<table align="center">
<tr>
<td>Name</td>
<td><input type="text" name="txt" id="txt" required ></td><td></td>
</tr>
<tr>
<td>Product</td>
<td>
<input type="checkbox" name="chk[]" value="class1">class1
<input type="checkbox" name="chk[]" value="class6">class6
<input type="checkbox" name="chk[]" value="class8">class8
<input type="checkbox" name="chk[]" value="class10">class10
<input type="checkbox" name="chk[]" value="class12">class12
<input type="checkbox" name="chk[]" value="engineering">engineering
<input type="checkbox" name="chk[]" value="technology">technology
</td><td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save"></td><td></td>
</tr>
</table>
</form>
My PHP VALUES
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$product = $_POST['chk'];
print_r($product);
foreach($product as $k=> $v)
{
if($v=="class1")
{
?>
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
if($v=="class1" && $v="class6")
{
?>
<form name="f1" action="" method="post">
<p>class1</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<p>class6</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit" name="submit" value="submit">
</form>
<?php
}
}
}
?>
**I want output like this:**For class1 form:
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<input type="submit" name="submit" value="submit">
</form>
****I want output like this:**For class1, class6 form:**
<form name="f1" action="" method="post">
<p>class1</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<p>class6</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit" name="submit" value="submit">
</form>
Like this the I want to make the form base on user selection.If user selected the all three classes(1,6,8), class 1 has different subject,like wise class 6 is different subject and engg has different subject.Now many problem is if user picks class(1,6,8),like wise class(1,6). How do I combine all the form element under one form and has one submit button
if(count($product) == 1){
if($product[0]=='class1'){
echo "class one form";
}else{
echo "class other forms";
}
}elseif(count($product) == 2){
if($product[0]=='class1' && $product[1]=='class6'){
echo "class one and class 6 form";
}else{
echo "class other forms";
}
}elseif(count($product) == 3){
if($product[0]=='class1' && $product[1]=='class6' && $product[2]=='class8'){
echo "class 1,6,8 form";
}else{
echo "class other forms";
}
}
Used else-if, somehow like this:
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$product = $_POST['chk'];
print_r($product);
foreach($product as $k=> $v){
if($v=="class1"){
?>
<form name="f1" action="" method="post">
<p>class1
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</p>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else if($v=="class1" && $v="class6"){
?>
<form name="f1" action="" method="post">
<p>class1</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<p>class6</p>
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
<input type="text" name="name" value="">Tutor Name
<input type="text" name="name" value="">Tutor Address
<input type="submit" name="submit" value="submit">
</form>
<?php
}}}
?>

Multiple checkbox values in php

I have the following code
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value"type1" /><br>
Type 2:<input type="checkbox" name="product[]" value"type2" /><br>
Type 3:<input type="checkbox" name="product[]" value"type3" /><br>
<input type="submit" value="Submit">
</form>
foreach($_POST["product"] as $value)
{
echo $value ;
}
it should return the values user have selected. But it gives only 'on' as output.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
An example code:
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="checkbox" name="product[]" value="type 1">
<input type="checkbox" name="product[]" value="type 2">
<input type="checkbox" name="product[]" value="type 3">
<input type="checkbox" name="product[]" value="type 4">
<input type="checkbox" name="product[]" value="type 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['product'])) {
foreach($_POST['product'] as $check)
{
echo $check;
}
}
?>
your value is wrong,you forgot =
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
It must be value="type1" not value"type1".
Try this
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
foreach($_POST["product"] as $value)
{
echo $value ;
}
?>
try this
<form id="myForm" action="" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST)) {
foreach($_POST["product"] as $value)
{
echo $value ;
}
}
First you forgot the = after value
Remodifying your script becomes this
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
$check = $_POST["product"]
foreach($check as $value)
{
echo $value ;
}

Get values from Dynamically created textboxes and checkboxes linked to textbox

I want to know how to capture values of dynamically created textboxes and checkboxes linked to each other.
My HTML elements would be like this:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" />
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
The checkboxes are linked to input with type="text"(i.e textbox)
Through JQuery i can clone the HTML elements in the "data_class" and append into it.
But while posting, how can we get the checkbox values related to each textbox.
i.e if i create 5 such elements like:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" value="A"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="B"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="C"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="D"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="E"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
How to get values that are checked for textbox with value "A", "B", "C", "D", "E" seperatly???
What I would do is to group them by sets, meaning:
One set given A:
A textbox, and 1 or more checkboxes:
So I would do it create a name="" attribute that would group them accordingly. Example:
This would be the initial markup:
<form method="POST">
<div class="data_class">
<div class="fieldset" data-group="A">
<input class="textbox_class" name="form_values[A][textbox]" type="text" />
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="1" />1
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="2" />2
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="3" />3
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="4" />4
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="5" />5
</div>
</div>
<button type="button" id="spawn">Spawn More</button><br/>
<input type="submit" name="submit" />
</form>
Spawning other fieldsets:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#spawn').on('click', function(){
var $initial = $('div.fieldset:last').clone();
// or just increment a number instead
var next = String.fromCharCode($initial.attr('data-group').charCodeAt(0) + 1);
$initial.children('.textbox_class').attr('name', 'form_values['+next+'][textbox]');
$initial.children('.checkbox_class').attr('name', 'form_values['+next+'][checkbox][]');
$('.data_class').append('<div class="fieldset" data-group="'+next+'">'+$initial.html()+'</div>');
});
});
</script>
So in PHP, after submission, it would come up with something like this:
if(isset($_POST['submit'])) {
$values = $_POST['form_values'];
echo '<pre>';
print_r($values);
echo '</pre>';
}
Output:
Array
(
[A] => Array
(
[textbox] => test1
[checkbox] => Array
(
[0] => 1
[1] => 2
)
)
[B] => Array
(
[textbox] => test2
[checkbox] => Array
(
[0] => 4
[1] => 5
)
)
)
Sample Demo
Note: This is just an example, if you want a simple key, just use a numeric one instead.

Categories