Get values from Dynamically created textboxes and checkboxes linked to textbox - php

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.

Related

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>

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

PHP Submit multiple questions in a quiz

I would like to create a html quiz with a dynamic number of questions and each question has a dynamic number of answers. My goal is to submit all of the questions when the quiz is completed. What changes to my HMTL do I need to make and what php do I use to get the array?
HTML
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions[]" type="hidden" value="1">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions[]" type="hidden" value="2">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
PHP
$questionID = $_POST['questions'];
$answers = $_POST['answers'];
foreach( $questionID as $i => $qid ) {
echo "The question id is ".$qid." and selected answer(s) is ".$answers[$i]. "<br>";
}
I believe one of my html problems has to do with the answers are named the same for both questions. Another php problem is which answer(s) were selected. I do not want to use AJAX as that causes more load on the server and I use JS for a quiz timer. Thanks for your help.
I think this will do what you want:
$questionAnswers = array('question1'=>array('answer1','answer2','answer3','answer4'));
$questionText = array('question1'=>'Question 1');
Displaying the form:
<form>
<?php foreach($questionAnswers as $q => $answers)
{
echo '<div class="questions"><strong class="white">'.$questionText[$q].'</strong>'
for($i = 0; $i < count($answers); $i++)
{
echo '<input name="'.$q.'[]" type="checkbox" value="'.$i.'"> <label>'.$answers[$i].'</label><br>';
}
echo '</div>';
}
?>
</form>
Processing submission:
$foreach($questionAnswers as $q => $answers)
{
$userAnswers = isset($_REQUEST[$q]) ? $_REQUEST[$q] : array();
...
}
You should specify the number of the question in the input name attribute, e.g.:
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions" type="hidden" value="1">
<input name="answers[1]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[1]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[1]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[1]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions" type="hidden" value="2">
<input name="answers[2]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[2]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[2]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[2]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[2]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[2]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
Then when you get a $_POST it will be like:
answers[1] = 'a',
answers[2] = 'b' ....

inserting multiple checkboxes names with codeigniter in to mysql

I want to make a timetable with, Codeigniter
but I have a problem with looping.
My view
Class
`<input type="checkbox" name="class[]" value="1A" id="1a" /> 1A
<input type="checkbox" name="class[]" value="1B" id="1b" /> 1B
<input type="checkbox" name="class[]" value="1C" id="1c" /> 1C `
Hours
<input type="checkbox" name="hours[]" value="1" id="h1" /> hours 1
<input type="checkbox" name="hours[]" value="2" id="h2" /> hours 2
<input type="checkbox" name="hours[]" value="3" id="h3" /> hours 3
<input type="checkbox" name="hours[]" value="4" id="h4" /> hours 4
<input type="checkbox" name="hours[]" value="5" id="h5" /> hours 5
<input type="checkbox" name="hours[]" value="6" id="h6" /> hours 6
Day
`<input type="checkbox" name="day[]" value="1" id="Monday" /> Monday
<input type="checkbox" name="day[]" value="2" id="Tuesday" /> Tuesday
<input type="checkbox" name="day[]" value="3" id="Wednesday" /> Wednesday
<input type="checkbox" name="day[]" value="4" id="Thursday" /> Thursday
<input type="checkbox" name="day[]" value="5" id="Friday" /> Friday
<input type="checkbox" name="day[]" value="6" id="Saturday" /> Saturday `
room
` A
<input type="checkbox" name="room[]" value="B" id="b" /> B
<input type="checkbox" name="room[]" value="C" id="c" /> C
<input type="checkbox" name="room[]" value="C" id="d" /> D `
my model
function add(){
$day= $this->input->post('day');
$hours= $this->input->post('hours');
$class= $this->input->post('class');
$room= $this->input->post('room');
foreach ($hoursas $hr){
$data = array('day'=>$day,'hours'=>$hr,'class'=>$class,'room'=>$room);
$this->db->insert('t_jadual', $data);
}
}
how do I loop $class,$room and $day?

How do I insert multiple checkbox values into database?

How do I insert multiple checkbox values into database? Here is my code, it workes for the radio buttons where there is only one value, but how do I insert more if I have checkboxes?
PHP CODE:
if(isset($_POST['radio']) &&
isset($_POST['checkbox'])){
$radio = $_POST['radio'];
$checkbox= $_POST['checkbox'];
$sql = "INSERT INTO info(radio, checkbox)VALUES ('$radio', '$checkbox')";
$result = mysql_query($sql);
}
HTML CODE:
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox" value="1">1<br>
<input type="checkbox" name="checkbox" value="2">2<br>
<input type="checkbox" name="checkbox" value="3">3<br>
<input type="checkbox" name="checkbox" value="4">4<br>
<input type="checkbox" name="checkbox" value="5">5<br>
<input type="checkbox" name="checkbox" value="6">6<br>
Html
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
Php
use a foreach to get all the values of checkboxes
//Do the necessary coding and then
if(!empty($_POST['checkbox']))
foreach($_POST['checkbox'] as $key=>$value){
if(trim($value) != ''){
//Do insertion here
}
}
}
change the names of yours checkboxes to:
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
on php do it:
$radio = $_POST['radio'];
$sql = 'INSERT INTO info(radio, checkbox)';
foreach($_POST['checkbox'] as $item){
$sql .= "VALUES('$radio', '$item')," ;
}
$sql = trim($sql, ','); //remove the last ',';
//performe all insert at once.
avoid to use mysql_* functions, try PDO
use php core functions serialize() and unserialize()

Categories