I'm making an online quiz system using PHP, I've saved questions, their options and answers in a table in database. In the html page, I'm printing the questions and options through a while loop (options are radio buttons). I learned how to send radio button values to PHP, but the problem I'm facing is that, only the selected option for the first question is sent to PHP. Like example, if I've saved two questions in database and they are printed on the page using a while loop, now when if I select option1 for question1 and option2 for question2, and click on submit, this just posts the value option1 to PHP, and the others selected button values are lost. I want all these value to check through database if the answers are correct and then display score based on correct answers.
Here is the code of my html page, I'm posting the whole code, but I think you should just check the <form> tag.
<body>
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>
<div id="container">
<div class="header">
<span class="heading"><span class="C">O</span>NLINE <span
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
<div class="logo"><img src="img/flogo.png" width="188" height="150"
alt="logo"></div>
</div>
<div class="center">
<nav>
<ul>
<li style="margin-right:180px; height:50px;width:auto">Username</li>
<li>Home</li>
<li>Results</li>
<li>Sign Out</li>
</ul>
</nav>
<div id="page-wrap">
<h1>HTML Quiz </h1>
<form action="results.php" method="post" id="quiz">
<ol>
<li>
<?php
while($result=mysql_fetch_assoc($record))
{
echo "<form>";
echo "<h3>";
echo "".$result['id'].". ".$result['question']."";
echo "</h3>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
echo '<label for="question-1-answer-A">';
echo "".$result['option1']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
echo '<label for="question-1-answer-B">';
echo "".$result['option2']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
echo '<label for="question-1-answer-C">';
echo "".$result['option3']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
echo '<label for="question-1-answer-D">';
echo "".$result['option4']."";
echo "</label>";
echo "</form>";
}
echo '<input type="Submit" value="Submit" name="submit" />';
?>
</li>
</ol>
</form>
</div>
</div>
<div class="footer">
<footer></footer> </div>
</div>
</body>
And the results.php
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
$result=mysql_fetch_assoc($record);
$total=0;
foreach($_POST['answer'] as $check) {
echo $check;
}
echo "YOUR SCORE: ".$total;
?>
I know this results file does nothing right now, the foreach part will be used to check every answer with the answer in database and then total will be incremented, the code you see right now is just to check how many values are being sent to PHP. Currently I've printed 6 questions, and after selecting options for every question, when I click submit, it displays
AYOURSCORE: 0
Here 'A' being the value of the option selected in "Question 1", values for other questions are lost. Which is the main issue I'm facing.
I've to submit this project in 2 days, so any help will be greatly appreciated.
Thanks
P.S. I am new to PHP` and have very little or no knowledge of java Script.
I think you are using two form. Where inside one form you repeating multiple for but in child form you have not provide action="". Or remove form from inside the loop.
<body>
<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>
<div id="container">
<div class="header">
<span class="heading"><span class="C">O</span>NLINE <span
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
<div class="logo"><img src="img/flogo.png" width="188" height="150"
alt="logo"></div>
</div>
<div class="center">
<nav>
<ul>
<li style="margin-right:180px; height:50px;width:auto">Username</li>
<li>Home</li>
<li>Results</li>
<li>Sign Out</li>
</ul>
</nav>
<div id="page-wrap">
<h1>HTML Quiz </h1>
<form action="results.php" method="post" id="quiz">
<ol>
<li>
<?php
while($result=mysql_fetch_assoc($record))
{
echo "<h3>";
echo "".$result['id'].". ".$result['question']."";
echo "</h3>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
echo '<label for="question-1-answer-A">';
echo "".$result['option1']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
echo '<label for="question-1-answer-B">';
echo "".$result['option2']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
echo '<label for="question-1-answer-C">';
echo "".$result['option3']."";
echo "</label>";
echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
echo '<label for="question-1-answer-D">';
echo "".$result['option4']."";
echo "</label>";
}
echo '<input type="Submit" value="Submit" name="submit" />';
?>
</li>
</ol>
</form>
</div>
</div>
<div class="footer">
<footer></footer> </div>
</div>
</body>
I think, that the problem is in name of the input: name="answer[]". It is the same name for every radiobutton - the same for all questions. I think, that adding an index into brackets will help
name="answer[1]" for question one
name="answer[2]" for question two
and so on.
The w3schools TryIt Editor lets you play with inputs.
Related
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
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>
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
i have been displaying two session messages in one page, rating. One message is displayed in every refresh showing the likes and dislikes. the other message in displayed once the rating form is submitted, the problem is once i submit the rating form the message displaying the rating isn't displayed. why is so??? can't i use two sessions in a page?? thanks
coding
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
sorry i think my code is a bit confusing
my actual code looks like this
<div class="top_model">
<h1>Model Ratings</h1>
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
<ul class="homemodel_thumbs">
<div style="text-align: right">
</div>
<?php
if(!empty($ratingArr)){
foreach($ratingArr as $ratings){
?>
<li><img src="<?php echo base_url();?>images/gallery/large/<?php echo $ratings['gallery_image'];?>" alt="<?php echo $ratings['gallery_image'];?>" class="center_image"/>
<p> </p>
<?php
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
//session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
</li>
<?php
}
}
?>
</ul>
this problem is solved. of the two session message for the one i used php session while for the another one i used code igniter session. sorry if i haven't mentioned i am using ci. anyways thanks everyone for the help :)
You're unsetting your rating message after you display it:
session_unset($_SESSION['rate_msg']);
This means it's not available after you've posted back the form unless you re-populate it again.
Remove this and i suspect it'll work.
You have an excess ?> :
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
if(!empty($_SESSION['rate_msg']))
{
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
You don't open php tag for last statement, (or unnesesery close it) it's treated as html
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?php
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
EDIT
<?php
// < HERE IS PHP >
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<!-- HERE IS HTML -->
<?
// < HERE IS PHP >
session_unset($_SESSION['error_msg']);
}
?> <!-- CLOSED PHP - NOW IT'S HTML - YOU SHOULD REMOVE IT -->
<!-- THIS IS NOT EXECUTED - BECAUSE IT'S HTML NOW -->
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?> <!-- THIS IS NOT PHP TAG BECASUE THERE IS NOT OPEN TAG -->
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
I have this code in the php file. it lists all the interests as checkboxes and allows to input a text field "other". The user should select several of these interests and save. Now, when the user clicks on the submit button it should take it to the controller and controller should extract them and store it in the database. I have one file thats coded to do that, but its no good and too confusing.
Here is the form in the normal php file
<form method="post" action="<?php echo site_url("userProfile/update_bio");?>" name="editForm" id="edit-form">
<div> <!-- description start -->
<div> <p <p class="special-p">Name:</p> <hr> </div>
<p class="used-p"> <input type="text" name="name" value="<?php echo $this->dx_auth->get_username(); ?>"></textarea> </p> <!-- should be real name -->
</div> <!-- description end -->
<div> <!-- description start -->
<div> <p <p class="special-p">Description:</p> <hr> </div>
<p class="used-p"> <textarea name="description"></textarea> </p>
</div> <!-- description end -->
<div> <!-- interests start -->
<div> <p class="special-p">Interests:</p> <hr class="special-hr2"> </div>
<p class="used-p">I am interested in:</p>
<ul>
<?php
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox" name="checks1[]" value="'.$row->id.'"/> '.$row->name.'</li>';
}
?>
<li><input type="checkbox" name="checks1[]" value="other"/> Other: <input type="text" name="otherText"/></li>
</ul>
</div> <!-- interests start -->
<div>
<input id="editbutton" type="submit" value="Save" name="editBioButton" class="small green button"/>
</div>
</form>
The controller function is empty. The problem is I don't know how will it receive the checkboxes.
Thanx in advance
foreach($this->input->post(checks1) as $check)
{
echo $check;
}
But I believe that if the checkbox isn't checked, it won't appear in the $_POST array. To combat this, give each checkbox a specific index:
<?php
$count=0;
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox"
name="checks1[' . $count . ']" value="'.$row->id.'"/> '.$row->name.'</li>';
$count++;
}
may do what you require. Your controller would be:
foreach($this->input->post(checks1) as $key => $value)
{
echo $key . ' - ' . $value;
}