I want to have a question that came from database (mysql) and able to answer 1 question at a time..
i was able to call the question but i have no idea on how can i make it a 1 question at a time
Here is the code
$sql = "SELECT question, answerA,answerB,answerC,answerD, correctAnswer FROM iq_question order by rand() LIMIT 20;";
$result = $conn->query($sql);
?>
<div id="first">
<label class="text-left mt-3" >Please choose the correct answer.</label><br>
<hr>
<form method="post" action="" id="form-data">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<center><h4>" .$row["question"]; "</h4></center>";
echo "<hr>";
echo "<p class='text-left'>";
echo "<input type='radio' name='question1ans' id='q1a' value='A'>";
echo "<label class='text-left' for='Q1A'>A. ".$row["answerA"];"</label></p>";
echo "<p class='text-left'>";
echo "<input type='radio' name='question1ans' id='q1b' value='B'>";
echo "<label for='Q1B'>B. ".$row["answerB"];"</label></p>";
echo "<p class='text-left'>";
echo "<input type='radio' name='question1ans' id='q1c' value='C'>";
echo "<label for='Q1C'>C. ".$row["answerC"];"</label></p>";
echo "<p class='text-left'>";
echo "<input type='radio' name='question1ans' id='q1d' value='D'>";
echo "<label for='Q1D'>D. ".$row["answerD"];"</label></p>";
echo "<p class='text-left'>";
echo "<hr>";
}
}
?> ````
Related
enter image description hereI am new to PHP and Mysql. i am trying to create a quiz project where multiple questions in a quiz will be inserted into the database once. How can i go about it. I have searched for solutions here and i tried using foreach. i am not having errors but it is not uploading
here is my form
<form class="quiz-questions">
<h1>Title Of Quiz</h1>
<?php
for($counter = 1; $counter <= $_SESSION['quiz_no']; $counter++)
{
echo "<div class='quiz-left'>";
echo "<div>";
echo "<label for='textarea' class='sn-quiz'>$counter</label><textarea name='question[]' class='textarea' id='example-three' placeholder='Enter the Question' cols='60' rows='3' ></textarea>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionA'>";
echo "<input type='text' name='optionA[]' placeholder='optionA'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionB'>";
echo "<input type='text' name='optionB[]' placeholder='optionB'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionC'>";
echo "<input type='text' name='optionC[]' placeholder='optionC'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionD'>";
echo "<input type='text' name='optionD[]' placeholder='optionD'>";
echo "</div>";
echo "</div>";
}
?>
<input type="submit" name="insert_quiz" class="btn-primary btn btn-lg" placeholder="SUBMIT">
</form>
and here is my submit code
<?php
if(isset($_POST['insert_quiz'])) {
$quiz_add_id = $_SESSION['quiz_id'];
$count = $_SESSION['quiz_no'];
$i = 0;
foreach ($_POST as $val) {
$question = $_POST['question'][$i];
$optionA = $_POST['optionA'][$i];
$optionB = $_POST['optionB'][$i];
$optionC = $_POST['optionC'][$i];
$optionD = $_POST['optionD'][$i];
$op1 = $_POST['op1'][$i];
$query = "INSERT INTO quizzes (quiz_add_id, quiz_question, quiz_option_A, quiz_option_B, quiz_option_C, quiz_option_D, quiz_option_correct)";
$query .= "VALUES ('{$quiz_add_id}', '{$question}', '{$optionA}','{$optionB}','{$optionC}','{$optionD}', '{$op1}')";
$create_quiz_query = mysqli_query($connection,$query);
$i++;
}
if(!$create_quiz_query ){
die('QUERY FAILED' . mysqli_error($connection));
}
}
?>
this is my first file code, where the php code is inside an html form and sending data by post method:
<form name="quiz" action=quizaction.php method="POST">
<?php
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
i want to use the $index variable in other file named quizaction.php file ,how do i ??
Use SESSION
<form name="quiz" action=quizaction.php method="POST">
<?php
session_start();//session starts here
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
// set session value and use $_SESSION['index'] at quizaction.php to find session value
if($index> 0){
$_SESSION['index'] = $index;
}
?>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
You can take hidden text-box in your form. then you can get that value in quizaction.php file.
while($row = $result->fetch_assoc()) {
echo "";
echo "Q:" . $row["question_name"]. "";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
echo "<input type='hidden' name='indexid' value='".$index."'/>
$index++;
echo $index;
}
I am creating an online examination system where am fetching questions and answers from database and displays options in radio button. But, the problem is if i click save it just saves one answer not answer from all questions
php codes for fetching questions
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans' value='" . $opa. "' onclick='answ(this.value)' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opb. "' onclick='answ(this.value)'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opc. "' onclick='answ(this.value)'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opd. "' onclick='answ(this.value)'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval' name='ansval' >";
echo "</form>";
echo " </div>";
}
Javascript that updates just one text box
<script>
function answ(answer) {
document.getElementById("ansval").value = answer;
}
</script>
The radio buttons name are all same for all questions. You have to make the radio buttons names dynamically for each answer set. You can use the following code :
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$questionId = $row['questionId']; //primary key of question table
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opa. "' onclick='answ(this.value,'<?php echo $questionId; ?>')' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opb. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opc. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opd. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval<?php echo $questionId; ?>' name='ansval<?php echo $questionId; ?>' >";
echo "</form>";
echo " </div>";
}
And You javascript function will be looks like :
<script>
function answ(answer,qId) {
document.getElementById("ansval"+qId).value = answer;
}
</script>
After the answer submit you can get the submitted values in same way. i.e $_POST['ansval'.$questionId]; Before get the value you have to create another loop to get all question Ids.
ID field must be unique
++$counter;
echo "";
$_POST['ansval1'],$_POST['ansval2'],$_POST['ansval3'].....
I'm trying to design a control page for my website that allows admins to delete requests to use a 3D printer at my school. The page displays a query of the schedule from the MySQL database and puts a delete button next to every entry. However, I would like to link the information in each entry to its respective delete button and have no idea how to do that. You can see the page itself here: http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php and this is my code for displaying the information
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th>Control</th>";
echo "<th>Student ID</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Date and Time</th>";
echo "<th>Description</th>";
echo "</tr>";
$key = 1;
while($row = $result->fetch_array()) {
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<tr>";
echo "<td><input name='".$row[5]."'type='submit' value='Delete' ></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No upcoming prints found!";
}
// free result set memory
$result->close();
}
I can't figure out how to link the information in a row with the delete button that I put in the row to send to the delete request.php program (which I have no code for yet)
you should put hidden field that hold each record id.
As below
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<input name='recored_id' type='hidden' value='".$row['id']."' >";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
Now from your request.php page you should do something like this
$recored_id = (int) $_POST['recored_id'];
$sql = "DELETE FROM table_name WHERE recored_id='$recored_id'";
Don't forget to give this code some security validation
all the best,
You're going to need some hidden inputs, like so:
while($row = $result->fetch_array()) {
echo "<tr>";
echo '<td><form method="POST" name="deleterequest" action = "deleterequest.php">';
echo "<input type='hidden' name='val0' value='" . $row[0] . "'>";
echo "<input type='hidden' name='val1' value='" . $row[1] . "'>";
echo "<input type='hidden' name='val2' value='" . $row[2] . "'>";
echo "<input type='hidden' name='val3' value='" . $row[3] . "'>";
echo "<input type='hidden' name='val4' value='" . $row[4] . "'>";
echo "<input name='".$row[5]."'type='submit' value='Delete' >";
echo "</form></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
You should change the name of these inputs though
I m trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.