How get the value from repetition form - php

I try to make a multiple choice for quiz, and the question is from the database and also the answer, so i code the program like this:
<form role="form" method="POST" action="test/hitung">
<div class="tab-content">
<?php
$no = 0;
foreach ($data_pilihan as $row){
$no++;
?>
<?php
if($no == 1){
$status = "active";
}else{
$status = "";
}
?>
<div class="tab-pane <?php echo $status;?>" role="tabpanel" id="<?php echo "step".$no?>">
<h3>Pertanyaan</h3>
<p>This is step 1</p>
<form action="">
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_a?>"><?php echo $row->pernyataan_a?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_b?>"><?php echo $row->pernyataan_b?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_c?>"><?php echo $row->pernyataan_c?><br>
<input type="radio" name="<?php echo "pertanyaan".$no?>" value="<?php echo $row->simbol_d?>"><?php echo $row->pernyataan_d?>
</form>
<ul class="list-inline" align="center">
<?php if($no != 1): ?>
<li><button type="button" class="btn btn-default prev-step">Previous</button></li>
<?php endif?>
<?php if($no != $Jumlah_Data):?>
<li><button type="button" class="btn btn-primary next-step">continue</button></li>
<?php endif?>
<?php if($no == $Jumlah_Data):?>
<li><button type="submit" class="btn btn-primary next-step">finish</button></li>
<?php endif?>
</ul>
</div>
<?php }
?>
<div class="clearfix"></div>
</div>
</form>
but when I try to get a data from the form value of the radio button and then I print, it just show the first value. How can i print or get another or rest of the value?

if all the name of the radio buttons are same , you will get only one value
As the Definition is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options
For eg: In this case user can select only one out of three options
<form action="" method="post">
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
From PHP you can access as - here u get only one selected value
<?PHP
$selected_radio = $_POST['gender'];
print $selected_radio;
?>
if you need multiple selection you can make use of checkbox
if your case if you need one options out of 4 need to be selected give a common name to radio button
please add if anything is missing

Related

How can I use checkboxes to delete rows with php and mysql

I created a table with database information and tried to create checkboxes to be able to delete lines more easily, but something is not working correctly.
I have a button with form:
<form action="delete-register.php" method="post">
<button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
<button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>
And I have rows with checkboxes:
<form action="delete-register.php" method="post">
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
</td>
</form>
And there is delete-register.php:
if (isset($_POST['delete'])) {
if (isset($_POST['selected'])) {
foreach ($_POST['selected'] as $id) {
$query = "DELETE FROM registers WHERE id = $id";
mysqli_query($conn, $query);
}
header('Location: registers.php');
exit;
}
}
The problem is that "selected" is always null and so nothing is deleted from the database.
How can I solve this problem?
Please note that the data submitted will be within the scope of <form>....</form>
Since you have two forms, when you click the submit button in the 1st form, it will not send the data of the 2nd form to the server.
Hence, please change the 2nd form to:
<form action="delete-register.php" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
<input type=submit name=delete>
</form>
[Additional Remark]
If you want to stick to using the 1st form to trigger the delete action, then please:
in the 1st form, change the delete from "submit" to "button"
add an onclick event to this delete button so that it will trigger submission of the 2nd form
make sure you have a hidden field known as "delete" in the 2nd form since you specified to have this field in your PHP script
you may have noticed that I have added id=form2 in the 2nd form so as to facilitate triggering of the submission by form1
So this is the revised code:
<form method="post">
<button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
<button type="button" name="delete" class="btn btn-secondary"
onclick='document.getElementById("form2").submit()';
><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>
<form id=form2 action="delete-register.php" method="post">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
<input type=hidden name=delete value="delete">
</form>

Posts are not saved through the checkbox

I have tried in many ways to fix it but without success.
I'm trying to sort categories through the checkbox but it does not work. The post is made public during editing but the checkbox is not active and after the post update is checkbox removes the id and the post loses from the category.
<form name="addpost" method="post" enctype="multipart/form-data">
<div class="form-group m-b-20">
<label for="exampleInputEmail1">Category</label>
<br>
<?php
$ret=mysqli_query($con,"select id,CategoryName from tblcategory where Is_Active=1");
while($result=mysqli_fetch_array($ret))
{
?>
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
<?php } ?>
<button type="submit" name="update" class="waves-effect waves-light pull-right butoni">Update </button>
</div>
</form>
This:
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
is placed in a loop... so there are more than one of these yes?!
We can't use the same id, and name for multiple inputs of a form or in html ... instead :
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
this will make things appear in one after the other...
if you want the box to be checked add an if in the loop
if($result['something'] should be checked){ ?>
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?> checked "><?php echo htmlentities($result['CategoryName']);?><br><?php
} else {
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br><?php
}
just fix the:
if($result['something'] should be checked){rest of the code}
to be correct according the when checkbox should be checked and when not.
For checkbox to be checked we add a checked in the input line.

How to check multiple radio buttons in array loop in php

I have fetched data and show in the radio buttons while the radio buttons are in loop, and insert the data in loop too.
The problem is that I can check only one radio button from the form, for each question one radio button must be check, which I can't.
<form method="POST" class="form-horizontal">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="radio" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="radio" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="radio" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="radio" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
The reason why you're getting that bug is you have given name="ans[]" so each time the index value incrementing.
You can fix it by giving the following code
<input type="radio" name="ans" value="1">
<input type="radio" name="ans" value="2">
And so on.
You have been using radio button inside a loop so you can use it by setting a loop variable like name="ans[$i]"
echo '<input type="radio" name="ans['.$i.']" value="1">
<input type="radio" name="ans['.$i.']" value="2">';

Checking the correct answer from the database by a checked radio button in PHP

I am creating an Online Assessment. I display all the questions randomly in a one page only. I have difficulty on how to check the correct answer in the database for checked radio button. I don't know what to do and the logic on how to do it.
This is my php codes for displaying the questions randomly,
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
This is my html codes with php codes,
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="1" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="2" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="3" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="4" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
Table name: questions
Table fields: QUESTION_NO, QUESTION, ANSWER_1, ANSWER_2, ANSWER_3, ANSWER_4, ANSWER
It is very easy
store Id of you questions in the value in the radio button and checkbox only those value are submitted which are checked , compare your code after submitting the form in next page , I hope you will understand, What i am trying to say
You should have database structure like this..
Table 1: Questions
Fields: que_id, question
Table 2: Answers
Fields: ans_id, que_id, answer, correct_ans, points
Table 3: Results
Fields: que_id, ans_id
When you add question, question and que_id will be stored n database.. Then you'll add multiple possible answers which are stored in Answers table with reference to que_id..
You need to change query for GUI to fetch question and answer from different table using join.
So GUI would be like this..
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
After that when user select an answer, ans_id will be saved along with the que_id in results table, from there you can manage all the information and comparison..
set Answerquestions to value for radio button
<input type="radio" value="ANSWER1" name="name">
<input type="radio" value="ANSWER2" name="name">
in php code
check the value submited with the correct Answer
get the correct Answer from db and save it in var
$query "SELECT correct_answer FROM TABEL_NAME";
and fetch query in var correct_a for example
$user_answer = $_POST['name'];
if($user_answer == $correct_a)
return true
else
return false

cannot save/retrieve value using html input in a foreach with php

For some probably obvious to anyone else reason, I cannot save the input value of my radio buttons and retrieve them with php. $_POST['answerToQuestion'] is not empty and will print the $key of each, but the value is empty. Can anyone readily see my mistake?
html:
<form action="answerQuestion.php" method="post">
<?php foreach($questions as $k => $q):
if(!$q['is_subquestion']):?>
<div class="questionAnswer">
<?php echo $q['body']; ?><br/>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" id="answer_yes" name="answerToQuestion[<?php echo $k; ?>]" value= 1>Yes</button>
<button type="button" class="btn" id="answer_no" name="answerToQuestion[<?php echo $k; ?>]" value= 0>No</button>
<button type="button" class="btn" id="answer_na" name="answerToQuestion[<?php echo $k; ?>]" value= 2>N/A</button>
</div>
<input type="hidden" id="hidden_2" name="answerToQuestion[<?php echo $k; ?>]" value="">
</div>
<?php endif;?>
<?php endforeach; ?>
<input type="submit" value="Next" name="submit-form" />
</form>
php:
foreach($_POST['answerToQuestion'] as $key=>$value)
{
echo ' '.$value.'<br/>';
}
Basing this answer on the question which clearly states "my radio buttons".
You don't need to echo the $k variable and you should use actual radio buttons:
<form action="answerQuestion.php" method="post">
<div class="btn-group" data-toggle="buttons-radio">
<label><input type="radio" name="answerToQuestion" value="1"> Yes</label>
<label><input type="radio" name="answerToQuestion" value="0"> No</label>
<label><input type="radio" name="answerToQuestion" value="2"> N/A</label>
</div>
<input type="submit" value="Submit">
</form>
Also, remove the spaces value= 0 (from your example), I use quotes as well.
$_POST['answerToQuestion'];
That should work.

Categories