Selection in Radio Button gets replaced in two divs - php

I have two questions with radio button (each question having 4 radio) in PHP
But there is single check in all 8 radio buttons, but it should be, with 4-4 radio buttons.
Look: https://imgur.com/Sytl72a
My Code:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
$lstmt = $user->runQuery("SELECT * FROM mcq WHERE LRN=:lrn ORDER BY Sr ASC ");
$lstmt->bindparam(":lrn",$id);
$lstmt->execute();
if($lstmt->rowCount() > 0)
{
$i=0;
while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
{
extract($lrow);
$i++;
?>
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
<hr></br></br>
<?php
}
}
?>
<button class="btn btn-large btn-primary" type="submit" name="btn-submit">Submit</button>
</form>
<?php
}
if(isset($_POST['btn-submit']))
{
echo $ufname = trim($_POST['radio']);
}
?>

Update your radio input name attribute just like this,
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio<?php echo $i; ?>?>" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
And for submitting:
<?php
}
$count = $i;
for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) {
echo $ufname = trim($_POST['radio'.$j]);
}
}
?>
It will definitely solve your problem,

Related

Making pages out of a text file using php with a next and previous button

I'm doing a php assignment which asks of us to make a quiz, which has 1 question per page, and cycles through five different questions picked at random through a next and previous button. My friend and I have been able to direct the code to go to other pages, but for some reason, the changing of question from 1 to the next does not work.
This is what we did in the body section of our work.
<body>
<div class="nav">
<ul>
<li><i class="fa fa-question-circle" aria-hidden="true"></i></i> Quiz
</li>
<li><i class="fa fa-list" aria-hidden="true"></i> Leaderboard</li>
<li><i class="fa fa-window-close" aria-hidden="true"> Exit</i></li>
</ul>
</div>
<br /><br />
<?php
$fp = fopen("questions.txt", "r");
$rowdata = "";
if ($fp) {
while (($buffer = fgets($fp, 10240)) !== false) {
$rowdata .= $buffer;
}
if (!feof($fp)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($fp);
}
$data = json_decode($rowdata);
if(isset($_POST['nickname']) && isset($_POST['topic'])) {
//random
$name = $_POST['nickname'];
$topic = $_POST['topic'];
$randomArray = [];
$counter = 1;
while($counter <= 5)
{
$random_val;
if($_POST['topic'] == "geography"){
$random_val= rand(0,9);
}
else{
$random_val= rand(10,19);
}
if(!in_array($random_val,$randomArray)){
array_push($randomArray,$random_val);
$counter++;
}
}
$selected_question_ids = [];
foreach ($randomArray as $val) {
array_push($selected_question_ids,$data[$val]->id);
}
//random end
if($_POST['topic'] == "geography"){
?>
<section>
<form action="result.php" method="POST">
<ol>
<?php
$q_counter = 1;
foreach($data as $row) {
if(in_array($row->id,$selected_question_ids))
{
?>
<li><?php echo $row->question?></li>
<ol>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->a?>"><?php echo $row->a?></li>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->b?>"><?php echo $row->b?></li>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->c?>"><?php echo $row->c?></li>
<input type="hidden" name="q_id_<?php echo $q_counter?>" value="<?php echo $row->id?>">
</ol>
<?php
$q_counter++;
}
}
?>
<input type="hidden" name="name" value="<?php echo $name ?>">
<input type="hidden" name="topic" value="<?php echo $topic ?>">
</ol>
<input type="button" name="prev" value="Prev" onclick="prev()">
<input type="button" name="next" value="Next" onclick="next()">
<button type="submit">Submit Quiz</button>
</form>
<script>
function next() {
<li><?php echo $row->question?></li>
<ol>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->a?>"><?php echo $row->a?></li>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->b?>"><?php echo $row->b?></li>
<li><input type="radio" name="<?php echo $q_counter?>"
value="<?php echo $row->c?>"><?php echo $row->c?></li>
<input type="hidden" name="q_id_<?php echo $q_counter?>" value="<?php echo $row->id?>">
</ol>
<?php
$q_counter++;
?>
}
</script>
</section>
<?php
}
if($_POST['topic'] == "history"){
?>
<section>
<form action="result.php" method="POST">
<ol>
<?php
$q_counter = 1;
foreach($data as $row) {
if(in_array($row->id,$selected_question_ids))
{
?>
<li><?php echo $row->question?></li>
Ans : <input type="text" name="<?php echo $q_counter?>" id="" required>
<input type="hidden" name="q_id_<?php echo $q_counter?>" value="<?php echo $row->id?>">
<br>
<br>
<?php
$q_counter++;
}
}
?>
<input type="hidden" name="name" value="<?php echo $name ?>">
<input type="hidden" name="topic" value="<?php echo $topic ?>">
</ol>
<input type="button" name="prev" value="Prev" onclick="<?php echo $row->question?>">
<input type="button" name="next" value="Next" onclick="<?php echo $row->question?>">
<button type="submit">Submit Quiz</button>
</form>
</section>
<?php
}
}
?>
</body>
And for what we expect, when we start the quiz, after we fill in the first question, we would be able to switch from question 1 to the next question by clicking on the next button, or go back to the previous one with the click of the previous button. Is there any idea how to work this? We are not able to make that function work.
Note: we put while($counter <= 5) as a temporary fix to show all 5 questions at once instead of 1 at a time for the time being. this should be changed to while($counter <= 1) by the end.

get value of radio button using jQuery and php

I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.
JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>

PHP form with radio buttons - make only one answer required, second optional

I have this PHP form with radio buttons:
{
$agreements = jm_get_application_setting( 'application_agreements', '' );
if( empty( $agreements ) ) return;
$questions = explode( "\n", $agreements );
if (!empty($questions)):
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="1" required><i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="0" required><i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
endforeach;
endif;
}
How can I make only one answer required to validate the form?
I mean.. Just the first MUST be selected, but I also need to show the second one for "legal reasons".
As is, this form accepts both choices as acceptable.
You can set a variable for the condition, and change it's value at the end of first iteration.
if (!empty($questions)):
$first = 1; // <----- HERE
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="1"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="0"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
$first = 0; // <----- HERE
endforeach;
endif;

Getting values of radio buttons generated in for loop

I'm currently running on codeigniter framework and running into some trouble. I am retrieving a list of questions and a list of their possible answers (something like a questionnaire) from the database and loading it out to participants to attempt.
Due to the the for loop I'm using, I'm unable to change the name or id value of individual questions. I sort of overcome this by appending the questionID to the name/id.
The trouble comes here when i post this over to my controller. How am I able to differentiate it when I need to insert it the results back into the database?
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" required value="A">A<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="B">B<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="C">C<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="true">True<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="false">False<br>
<?php } else {?>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="ok">OK<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="agree">Agree<br>
<?php }?>
<?php } ?>
The above is the way I have printed the questionnaire out, any possible solutions for this?
Thats hard to manage having a name attribute like that.
Use a grouping name attribute:
name="answers[<?php echo $row->QuestionID;?>]"
Simple example:
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" required value="A">A<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="B">B<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="C">C<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="true">True<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="false">False<br>
<?php } else {?>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="ok">OK<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="agree">Agree<br>
<?php }?>
<?php } ?>
So that in PHP, you could just call it by its name:
public function controller_name()
{
$answers = $this->input->post('answers');
foreach($answers as $question_id => $answer) {
}
}

isset multiple check boxes in php from mysql database

i am populating a form from mysql. code is. .
task.php
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
now in cars.php i want to make a query to display the details of car selected,
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
echo $_POST[$cname];?????????????
}
?>
</div>
now how to process the form here in cars.php ?
thanks
Make car attribute array::
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
and get them on next page with
if(isset($_POST['submitcars'])){
foreach($_POST['car'] as $car){
// do something with $car
}
}
Try this
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
Firstly, let me preface this by saying that you shouldn't be using the mysql functions. They're depreciated and no longer recommended.
On task.php, your opening form tag was being printed inside your loop. Secondly, you should probably use an array of checkboxes using [].
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="cars[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php } ?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
Then, on cars.php:
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
if(isset($_POST['cars'] && count($_POST['cars']) > 0){
foreach($_POST['cars'] as $key => $car){
echo $car . '<br /'>;
}
}
else{
echo 'No cars found!';
}
}
?>
</div>

Categories