I want to show the form elements based on user selection - 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. 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
}}}
?>

Related

php+html multiple row submit with checkbox filter

I created a page for multiple rows submit data to mysql with php!
But, I need filter check the checkbox[] has been checked for submit current row data
In my demo,
If I checked the row2 and row3, I expected I will get id=2 & id=3
finally I get the id=1 & id=2
In the same situation, if I checked row3 only, I will get the id=1
I probably understand the principle, but I really can’t find a solution
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
if (#$_POST['checked'][$key] == "on") {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
}
print_r($row);
?>
<form action="" method="POST">
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">submit</button>
</form>
I try #CBroe
if checked row3, I still get a
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
$row[$key]['checkbox'] = $_POST['checkbox'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="checked[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">Submit</button>
</form>
#CBroe Thanks for your
<?php
$row = "";
if ($_POST) {
foreach ($_POST["id"] as $key => $v) {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="id[1]" value="1">
<input type="text" name="other_value[1]" value="a">
</p>
<p>
<input type="checkbox" name="id[2]" value="2">
<input type="text" name="other_value[2]" value="b">
</p>
<p>
<input type="checkbox" name="id[3]" value="3">
<input type="text" name="other_value[3]" value="c">
</p>
<button type="submit">submit</button>
</form>

How do I show the form based on my checkbox value

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>

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
}

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

Categories