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">';
Related
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.
Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked.
I can't use " input type="hidden" name="chk_name[]" value="1" " because of the array. It doubles my array.
If I try to set it after using isset it doesn't put it into my array.
I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.
So how can I put a 1 into the array for wherever it isn't checked?
Form:
<?php
require_once ("con.php");
$p=payment();
$result = mysqli_query($mysqli,$p);
while($res=mysqli_fetch_array($result))
{
?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]"
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>
php:
$paid=$_POST['paid'];
$active=$_POST['active'];
foreach($_POST as $key=>$value)
{
$ID=$ID[$key];
$paid=$paid[$key];
$active=$active[$key];
$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up);
echo "Information stored successfully";
}
?>
my function:
function updatePayment($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
:Updated code:
I can see the arrays are coming out fine for everything now. The hidden method worked.
Thanks for everyone's help!
As soon the hidden field and the checkbox has the same name it will work no matter if the name is array or not.
The simple reason for this is that when is unchecked the checkbox value is not posted.
Try this code and you will see the result
<pre>
<?php
print_r($_POST);
?>
</pre>
<form method="POST">
<input type="hidden" name="fields[1]" value="1"/>
<input type="checkbox" name="fields[1]" value="0"/>
<input type="hidden" name="fields[2]" value="0"/>
<input type="checkbox" name="fields[2]" value="1"/>
<input type="hidden" name="fields[3][something][else]" value="3"/>
<input type="checkbox" name="fields[3][something][else]" value="3"/>
<button type="submit">Submit</button>
</form>
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
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.
I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value.
I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:
$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}
Thanks
It's pretty simple, take a look at the code below:
The form:
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
PHP code:
<?php
$answer = $_POST['ans'];
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
A very more efficient way to do this in php:
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
and for check boxes multiple choice:
<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>
Just simply use isset($_POST['radio']) so that whenever i click any of the radio button, the one that is clicked is set to the post.
<form method="post" action="sample.php">
select sex:
<input type="radio" name="radio" value="male">
<input type="radio" name="radio" value="female">
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['radio'])){
$Sex = $_POST['radio'];
}
?>
<?php
if (isset($_POST['submit']) and ! empty($_POST['submit'])) {
if (isset($_POST['radio'])) {
$radio_input = $_POST['radio'];
echo $radio_input;
}
} else {
}
?>
<form action="radio.php" method="post">
<input type="radio" name="radio" value="v1"/>
<input type="radio" name="radio" value="v2"/>
<input type="radio" name="radio" value="v3"/>
<input type="radio" name="radio" value="v4"/>
<input type="radio" name="radio" value="v5"/>
<input type= "submit" name="submit"value="submit"/>
</form>
take a look at this code
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
php
<?php
if(isset($_POST['submit'])){
if(isset( $_POST['ans'])){
echo "This is the value you are selected".$_POST['ans'];
}
}
?>
I suggest you do it through the GET request:
for example, index.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="button" value="submit" onclick="sendPost()" />
</form>
<script type="text/javascript">
function sendPost(){
var value = $('input[name="ans"]:checked').val();
window.location.href = "sendpost.php?ans="+value;
};
</script>
this is sendpost.php:
<?php
if(isset($_GET["ans"]) AND !empty($_GET["ans"])){
echo $_GET["ans"];
}
?>
my form:
<form method="post" action="radio.php">
select your gender:
<input type="radio" name="radioGender" value="female">
<input type="radio" name="radioGender" value="male">
<input type="submit" name="btnSubmit" value="submit">
</form>
my php:
<?php
if (isset($_POST["btnSubmit"])) {
if (isset($_POST["radioGender"])) {
$answer = $_POST['radioGender'];
if ($answer == "female") {
echo "female";
} else {
echo "male";
}
}else{
echo "please select your gender";
}
}
?>