Only one radio buttons being selected out of four questions - php

I want to make a list of four questions and four options for each question. I have successfully fetched the questions with foreach loop but, radio buttons do not seem to work with foreach loop.
Eg: I chose one answer form the first question and jump to second, but if I select an answer for the second question, the selected option of the first questions gets deselected. I have tried changing values of options, that did not work, I tried using for loop inside the foreach loop and even that did not work.
Following is my code:
<form method="post" action="process/quiz.php">
<?php
$quizList = $quiz->getQuiz(4);
if($quizList){
foreach($quizList as $list){
?>
<div class="row rowpadding">
<div class="col-md-8 col-md-offset-2" id="panel1">
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<?php echo $list->title; ?>
</h5>
</div>
<div class="panel-body two-col">
<div class="row">
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-1" name="ans1" value="<?php echo $list->option_A ?>">
<label for="radio-button-1">
<span class="frb-title"><?php echo $list->option_A ?> </span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-2" name="ans2" value="<?php echo $list->option_B ?>">
<label for="radio-button-2">
<span class="frb-title"><?php echo $list->option_B ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-3" name="ans3" value="<?php echo $list->option_C ?>">
<label for="radio-button-3">
<span class="frb-title"><?php echo $list->option_C ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-4" name="ans4" value="<?php echo $list->option_D ?>">
<label for="radio-button-4">
<span class="frb-title"><?php echo $list->option_D ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
?>
<div class="panel-footer rowpadding">
<div class="row">
<div class="col-md-6">
<button type="submit" class="btn btn-sm btn-block ">
<span class="fa fa-send"></span>
submit </button>
</div>
</div>
</div>
</form>
Is there anything that I am missing?

Your problem is that you are re-using the names and the IDs of your inputs. Names and IDs have to be unique for the HTML to be valid, and to work as you intend it to. You can have the input-names as HTML arrays instead, and group by that.
Using the $key of the array, you can define a unique name for each your group of answers. We also use this to define the IDs of your elements, since they must be unique.
Changes made are,
Include the $key in the loop
Adding -<?php echo $key; ?> to all instances where you use the ID of the buttons (and the reference in the label), to ensure all IDs are unique
Using name="answer[<?php echo $key; ?>]" instead of ans1, ans2, ans3, ans4. This ensures that only one radio button can be selected per answer, and that you have one array of answers, each element being the answer of one question.
foreach ($quizList as $key=>$list){
?>
<div class="row rowpadding">
<div class="col-md-8 col-md-offset-2" id="panel1-<?php echo $key; ?>">
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<?php echo $list->title; ?>
</h5>
</div>
<div class="panel-body two-col">
<div class="row">
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-1-<?php echo $key; ?>" name="answer[<?php echo $key; ?>]" value="<?php echo $list->option_A ?>">
<label for="radio-button-<?php echo $key; ?>">
<span class="frb-title"><?php echo $list->option_A ?> </span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-2-<?php echo $key; ?>" name="answer[<?php echo $key; ?>]" value="<?php echo $list->option_B ?>">
<label for="radio-button-2-<?php echo $key; ?>">
<span class="frb-title"><?php echo $list->option_B ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-3-<?php echo $key; ?>" name="answer[<?php echo $key; ?>]" value="<?php echo $list->option_C ?>">
<label for="radio-button-3-<?php echo $key; ?>">
<span class="frb-title"><?php echo $list->option_C ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
<div class="col-md-6">
<div class="frb frb-danger margin-bottom-none">
<input type="radio" id="radio-button-4-<?php echo $key; ?>" name="answer[<?php echo $key; ?>]" value="<?php echo $list->option_D ?>">
<label for="radio-button-4-<?php echo $key; ?>">
<span class="frb-title"><?php echo $list->option_D ?></span>
<span class="frb-description"></span>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
Now, when you submit the form, the selected answers will be in an array where the name is answer. So you will have to do something like
foreach ($_POST['answer'] as $key=>$value) {
// $key is the same key from the loop above
// $value is the value of the selected radio button
}

Radio buttons are tied together by name. In your foreach(), you keep repeating the same names for each set of question answers. (You're also repeating the same ID, which is bad form, but won't break your script).
You need to restructure your radio buttons so that each group of buttons (that belong together) have the same name. And that name has to be unique per group.
A simplified example:
<form>
<p>These belong together, and all have the name "gender":</p>
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<p>These belong together, and all have the name "team":</p>
<input type="radio" name="team" value="blue"> Blue<br>
<input type="radio" name="team" value="red"> Red<br>
</form>

An even more simplified answer
foreach($quizList as $key => $list){ ?>
<form>
<input type="radio" id="radio-button-1" name="answer[<?php echo $key;?>]" value="<?php echo $list->option_A ?>"> <!-- answer_0 -->
<input type="radio" id="radio-button-1" name="answer[<?php echo $key;?>]" value="<?php echo $list->option_B ?>"> <!-- answer_0 -->
</form>
Then in php you should get something like this:
$_POST['answer'] = [
'0' => 'foo'
//'1' => 'biz' ....
];
With Ajax
One note with numbered keys. IF you use AJAX (if not you can basically ignore this) you may lose numeric indexes when converting to and from JSON, for example imagine we expect this:
$_POST['answer'] = [
'0' => 'foo'
'2' => 'biz' ....
];
When that is encoded in Json it will likly be something like this (where did the keys go)
'{"answer":["foo", "biz"]}`
Then when PHP converts that back we have lost our keys. And we we'll have something like this:
$_POST['answer'] = [
0 => 'foo'
1 => 'biz' ....
];
This is also true of any array function that doesn't preserve keys, sort etc. The easy solution here is to just prefix the key with something like a or _ even. Then they will be strings and translate to objects in the JSON. In PHP you could still match these like this:
if("a$id" == $post_id){}
if(substr($post_id,1) == $id){}
//remove all prefixes
print_r(array_combine(preg_replace('/^a/', '', array_keys($answers)),$answers));
//it feels wrong but if you have to append you can do this
var_dump((int)'2a' == 2); //true so your key would be 2a because how PHP converts strings to ints.
And so on.
Hope it helps!

Related

How to get input radio name with many different name in laravel

In this project, i make something like online test. my question is how to get radio name with many different name in laravel ? i use id to different their name so but i cant get their name in controller.
this is my view
<form action="{{url('kumpulkan')}}" method="POST" class="responsive-form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#if(Session::get('jurusan') == Multimedia)
<?php
$no = 1;
;?>
#foreach($soal_multimedia as $row)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="wrapper-soal">
<div class="pertanyaan">
<span class="no-soal"><?php echo $no++ ;?>. </span> <span class="teks-soal">{{$row->soal}}</span>
</div>
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
</div>
</div>
</div>
#endforeach
#endif
<button class="btn btn-default btn-lg">Selesai</button>
</form>
this is my controller
public function Postkumpulkan(Request $request)
{
$insert = array();
$insert['id_soal'] = $request->get('id_soal');
$insert['nama'] = Session::get('nama');
$insert['no_peserta'] = Session::get('no_peserta');
$insert['sekolah'] = Session::get('sekolah');
$insert['jurusan'] = Session::get('');
$insert['jawaban'] = $request->get('jawaban_{{$row->id}}');
}
i think you would better use checkbox and instead of this naming jawaban_{{$row->id}}
use this kind jawaban[{{$row->id}}] in this case you can get all jawabans in one array
like this :
$insert['jawaban'] = $request->get('jawaban');
Since I assume you intend to make your radio buttons to work as a radiogroup (yes, where you can only choose 1) and since that is how radio buttons work, you should just give the same name for all the radio buttons.
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
Then you can just retrieve it in the controller:
$insert['jawaban'] = $request->get('jawaban');
It will retrieve the chosen radio button.

How can i fetch if there are two different types are there in database ex-(type=1,type=2) same table

I have database table as os_dish
in that dish type=1 is tiffen // dishtype=2 is lunch//
My error is only tiffen values are coming in both Tiffen And Lunch
i want to select multiple how can i do that this is my Tiffen code:
<div class="products-row">
<?php $tq=$conn->query("select * from os_dish where dish_type=1");
while ($tiffen = $tq->fetch_assoc()) {
?>
<div class="col-md-3">
<div class="foodmenuform row text-center">
<input type="checkbox" id="<?php echo $tiffen['dish_name'];?>" class="Foodmenu" value="<?php echo $tiffen['dish_name'];?>" name="tifeen[]" hidden>
<label for="<?php echo $tiffen['dish_name'];?>"><img src="img/dish/<?php echo $tiffen['dish_image']; ?>" class="img img-responsive" /></label>
</div>
</div>
<?php } ?>
</div>
And this is my Luch code:
<div class="products-row">
<?php $lq=$conn->query("select * from os_dish where dish_type=2");
while ($lunch = $lq->fetch_assoc()) {
?>
<div class="col-md-3">
<div class="foodmenuform row text-center">
<input type="checkbox" id="<?php echo $lunch['dish_name'];?>" class="FoodMenu" value="<?php echo $lunch['dish_name'];?>" name="lunch[]" hidden>
<label for="<?php echo $lunch['dish_name'];?>"><img src="img/dish/<?php echo $lunch['dish_image']; ?>" class="img img-responsive" /></label>
</div>
</div>
<?php } ?>
</div>

Displaying multiple sql columns in one html column

I have a form with multiple checkboxes which are stored as different columns in a database.
I want to display them as a single column in a webpage, how can I do that?
HTML Form:
<div class="form-group">
<label class="col-md-2 control-label">R-11</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="r11" value="R11">
</div>
<label class="col-md-2 control-label">E-1</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="e1" value="E1">
</div>
<label class="col-md-2 control-label">E-2</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="e2" value="E2">
</div>
<label class="col-md-2 control-label">E-3</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="e3" value="E3">
</div>
<label class="col-md-2 control-label">L-1</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="l1" value="L1">
</div>
<label class="col-md-2 control-label">R-12</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="r12" value="R12">
</div>
<label class="col-md-2 control-label">C-1</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="c1" value="C1">
</div>
<label class="col-md-2 control-label">C-2</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="c2" value="C2">
</div>
<label class="col-md-2 control-label">C-3</label>
<div class="col-md-1">
<input type="checkbox" class="form-control" name="c3" value="C3">
</div>
</div>
PHP Code, generating the rows and columns:
<?php
while($maininfo=mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$maininfo['run_number']."</td>";
echo "<td>".$maininfo['date']."</td>";
echo "<td>".$maininfo['station']."</td>";
echo "<td>".$maininfo['time_of_call']."</td>";
echo "<td>".$maininfo['onscene']."</td>";
echo "<td>".$maininfo['inservice']."</td>";
echo "<td>".$maininfo['address']."</td>";
echo "<td>".$maininfo['category1']."</td>";
echo "<td>".$maininfo['category2']."</td>";
echo "<td>".$maininfo['info']."</td>";
echo "<td>".$maininfo['shift']."</td>";
echo "<td>".$maininfo['name']."</td>";
echo "<td>".$maininfo['r11']."</td>";
echo "<td>".$maininfo['e1']."</td>";
echo "<td>".$maininfo['e2']."</td>";
echo "<td>".$maininfo['e3']."</td>";
echo "<td>".$maininfo['l1']."</td>";
echo "<td>".$maininfo['r12']."</td>";
echo "<td>".$maininfo['c1']."</td>";
echo "<td>".$maininfo['c2']."</td>";
echo "<td>".$maininfo['c3']."</td>";
echo "</tr>";
}//end while
?>
I would like the table in the website to show all of the r-11 thru c-3 in one column. How can I do that? I have tried merging a couple of them together like:
echo "<td>".$maininfo['r11', 'e1']."</td>";
However, that doesn't work.
This represents the output from one database column:
$maininfo['r11']
This is two:
$maininfo['r11'] . $maininfo['e1']
Wrap those two in an HTML table cell:
"<td>" . $maininfo['r11'] . $maininfo['e1'] . "</td>"
Repeat as necessary for however you want to design the HTML output. Add spaces, separators, more HTML markup, etc.
Basically, the values are still individual things. You simply need to concatenate the output together into the HTML structure you want. It's not up to the database to do this, or the array structure. They have their own jobs to do. It's up to you to build your output strings.
echo "<td>".$maininfo['r11'].$maininfo['e1']."</td>";

Getting only half of an array after passing the form

I have two forms. After submitting first form it comes to a second form where i need to get values from first form. Here is the first part of form:
<?php $result2 = getProcessID();?>
<form class="formcss" method="post" action="create2.php" >
<div class="row">
<div class="small-8 large-8 columns">
<label>Process: <small style="color:red;">*</small>
<div class="multiselect">
<div class="selectBox">
<select onclick="showCheckboxes()" class="input input1 name">
<option>-- Select a process -- Add number of people reqired --</option>
</select>
<label class="tool tool1" for="name" style="top:-15px; margin-left:-14px; left:-207px; ">Choose process and number <br> of people required</label>
<div class="overSelect"></div>
</div>
<div class="scrollable" id="checkboxes">
<?php
while ($row = mysql_fetch_array($result2))
{
$row[0] = cleanOutputData($row[0]);
?>
<div class="row">
<div class="small-6 large-6 columns">
<label style="height: 38px;">
<input type='checkbox' style="width:20%;" name='process[]' id=<?php echo $row[0] ?> value=<?php echo $row[0] ?>/><?php echo $row[0] ?>
</label>
</div>
<div class="small-6 large-6 columns">
<label><input type='text'style="margin-left:50%; width:20%;" name='number[]'/>
</label>
</div>
</div>
<?php
}
mysql_free_result($result2);
?>
</div>
</div>
</label>
</div>
</div>
</form>
It seems fine to display the values in the second form except two problems:
1) Only half of the process is displayed
2) I can the number only for first 3 processes, but for other processes it stays an empty field.
Here is my second form:
$_SESSION['process'] = $_POST['process'];
$_SESSION['number'] = $_POST['number'];
$proc=$_SESSION['process'];
$len = count($proc); // getting length of an array
$num=$_SESSION['number'];
<form class="formcss" method="post" action="create.php#err" id="reportform" enctype="multipart/form-data">
<div id="project" class="small-9 large-9 columns">
<label style="font-size: 40px; margin-left:10%;">Project <?php echo $_SESSION["title"]; ?></label>
<label><?php for($y=0;$y<$len;$y++)
{
echo "Process: "."$proc[$y]"."<br />";
echo "People required: "."$num[$y]"."<br />";
?>
<ol>
<li class="placeholder" <?php if (isset($leader)) echo 'value="'.$leader.'"' ?>>Add people here</li>
</ol>
<?php
}
?>
</label>
<br><br><br>
<div class="row">
<input type = "submit" style="margin-left:300px; width:150px;" name ="submit" class="button" value = "Create Project" onclick="userSubmitted = true;"/>
</div>
</div>
</form>
As i said only half of the process is shown, for example, I have 'ANM KEY' process, and when i pass the value and display it on another form it displays only 'ANM'. Also I want to display number of people required for each process, but it works only for first three rows. So how i can get the wholename of the process, and display number of people required. Thank you!

How to Get the multiple radio buttons values in PHP which having names dynamically

I am having following codings in my form..how do I get the value of all radio button values on submit which is inside looping? Or give me any other solution for this.
<form action="res.php" method="post">
<?php
for($i=1;$i<=5;$i++)
{
?>
<div class="well well-sm well-primary">
<input type="hidden" name="ques"/>Questions?
</div>
<div class="well well-sm">
<div class="radio">
<label>
<input type="radio" name="optradio<?php echo $i; ?>" value="a">Option 1
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio<?php echo $i; ?>" value="b">Option 2
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio<?php echo $i; ?>" value="c">Option 3
</label>
</div>
</div>
<?php
}
?>
<button type="submit" class="btn btn-success" name="submit">Finish</button>
</form>
Use array of radio button as follows
<form method="post">
<?php
for($i=1;$i<=5;$i++)
{
?>
<div class="well well-sm well-primary">
<input type="hidden" name="ques"/>Questions?
</div>
<div class="well well-sm">
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="a">Option 1</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="b">Option 2</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="c">Option 3</label>
</div>
</div>
<?php
}
?>
<button type="submit" class="btn btn-success" name="submit">Finish</button>
</form>
To access the posted values, you can simply use $_POST['optradio']
Considering the selection for 5 questions to be Option 1, Option 2, Option 3, Option 1, Option 2
POST['optradio'] will give array like
Array ( [1] => a [2] => b [3] => c [4] => a [5] => b )
To access sigle values from this array, you can use foreach loop as,
<?php
foreach($_POST['optradio'] as $option_num => $option_val)
echo $option_num." ".$option_val."<br>";
?>
take a one hidden input for storing radio button name array in for loop
like
<input type="hidden" name="testradio[]" value="optradio<?php echo $i; ?>">
and then fetch radio button value using foreach
$rdobtn = $_POST['testradio'];
$idx = 0;
foreach($rdobtn as $val){
$rdovalue = $val[$idx];
// perform opertation using above $rdovalue variable.
$idx++;
}
}
Yes, as Sean commented, try this:
<form action="res.php" method="post">
<?php
for($i=1;$i<=5;$i++)
{
?>
<div class="well well-sm well-primary">
<input type="hidden" name="ques"/>Questions?
</div>
<div class="well well-sm">
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="a">Option 1
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="b">Option 2
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optradio[<?php echo $i; ?>]" value="c">Option 3
</label>
</div>
</div>
<?php
}
?>
<button type="submit" class="btn btn-success" name="submit">Finish</button>
</form>
and then use the below in PHP side to get radio button value as :
foreach ($_POST['optradio'] as $optNum => $option) {
// do stuff with $optNum and $option
}
Try this:
<input type="radio" name="optradio[]" value="a">
And in PHP file,
$_POST['optradio'] will result as an array.

Categories