PHP Quiz System - php

I am working on a PHP Quiz System which displays all questions on one page.
The following is my code.
PHP
$getQ = mysqli_query($condb, "select * from questions where eid = '$exam'");
if($getQ){
if(mysqli_num_rows($getQ)==0){
$summaryLink = 'examSummary.php?exam='.$exam;
redirect($summaryLink);
}
Form
<?php while($ans = mysqli_fetch_array($getQ)){ ?>
<div class="ques" id="ques">
<input type="hidden" name="exam" value="<?php echo $exam; ?>">
<input type="hidden" name="question" value="<?php echo $token; ?>">
<input type="hidden" name="qid" id="qid" value="<?php echo $ans['qid'];?>">
<input type="hidden" name="sid" id="sid" value="<?php echo $_SESSION['sid']; ?>">
<p>
<input type="radio" name="answer" value="1" id="answer" onChange="update();">
<label for="<?php echo $ans['option_1'];?>"><?php echo $ans['option_1'];?></label>
</p>
<p>
<input type="radio" name="answer" value="2" id="answer" onChange="update();">
<label for="<?php echo $ans['option_2'];?>"><?php echo $ans['option_2'];?></label>
</p>
<p>
<input type="radio" name="answer" value="3" id="answer" onChange="update();">
<label for="<?php echo $ans['option_3'];?>"><?php echo $ans['option_3'];?></label>
</p>
<p>
<input type="radio" name="answer" value="4" id="answer" onChange="update();">
<label for="<?php echo $ans['option_4'];?>"><?php echo $ans['option_4'];?></label>
</p>
<p>
<input type="radio" name="answer" value="5" id="answer" onChange="update();">
<label for="<?php echo $ans['option_5'];?>"><?php echo $ans['option_5'];?></label>
</p>
<div class="status2"></div>
What I want to do is, once the user select an answer for a questions, the selected option and few other hidden form data (qid, eid, sid) should be passed to updateAnswer.php
updateAnswer.php will update the selected answer to the database.
I tired the following jQuery. But it wont do anything.
<script>
function update() {
var radVal = $("input[type=radio][name=answer]:checked").val();
jQuery.ajax({
url: "updateAnswer.php",
data:'eid='+$("#eid").val()+'&sid='+$("#sid").val()+'&qid='+$("#qid").val()+'&answer='+$(radVal).val(),
type: "POST",
success:function(data){
$(".status2").html(data);
},
error:function (){}
});
}
</script>
The system should pass data to updateAnswer.php when a user clicks on an option(answer).
But with my current code, when a user clicks on an option won't do anything.

So, there are some mistakes in your code, first one is that the id should always be unique, so before your while loop take a variable and initialize it to zero and increment it on every iteration; You can use this variable and append after every id to make it unique. Like so -
<?php
$i = 0;
while(bla bla){
$i++;
?>
<!-- You can also use question id here ↓↓ which should be unique -->
<input type="hidden" name="qid" id="<?php echo "qid".$i; ?>" value="<?php echo $ans['qid'];?>">
<!-- session id will be the same -->
<input type="hidden" name="sid" id="sid" value="<?php echo $_SESSION['sid']; ?>">
<!-- same for others -->
<?php
}
?>
You also need to give this($i or question-id(whichever you choose)) as an attribute to radio buttons to identify which question id we need to get and on update function you need to pass the current checked element, like so -
<input type="radio" name="answer" value="1" class="answer" onChange="update(this);" data-id="<?php echo $i; ?>">
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="<?php echo $i; ?>">
<!-- same for others -->
I've created a working snippet for your problem(without PHP and AJAX of course). I've given them unique id and values, I'm printing the desired values on the console. I've changed the AJAX code as well, you can use it in your code to check if it works. Comments are mentioned wherever necessary, see if it helps you.
function update(element) {
let answer = $(element).val(); // get the value of checked element
let id = $(element).attr('data-id'); // get id of the radio button clicked
let eid = $('#eid').val(); // exam id will be same for all the questions
let sid = $('#sid').val(); // session id will be same
let qid = $('#qid' + id).val(); // get question id
// send ajax request
/*jQuery.ajax({
url: "updateAnswer.php",
data: 'eid=' + eid + '&sid=' + sid + '&qid=' + qid + '&answer=' + answer,
type: "POST",
success: function(data) {
$('#status' + id).html(data); // show status on the related div
},
error: function() {}
});*/
console.clear();
console.log(`id: ${id}, eid: ${eid}, sid: ${sid}, qid: ${qid}`)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ques" id="ques1">
<input type="hidden" name="exam" value="some_exam" id="some_exam">
<input type="hidden" name="eid" value="some_examid" id="eid">
<!-- question id 1 (use php) -->
<input type="hidden" name="question" id="question1" value="question1">
<input type="hidden" name="qid" id="qid1" value="qid1">
<input type="hidden" name="sid" id="sid" value="some_session">
<p>
<!-- Give question id in data-id attribute (use php) and pass current element in update function -->
<input type="radio" name="answer" value="1" class="answer" onChange="update(this);" data-id="1">
<label>option_1_1</label>
</p>
<p>
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="1">
<label>option_1_2</label>
</p>
<p>
<input type="radio" name="answer" value="3" class="answer" onChange="update(this);" data-id="1">
<label>option_1_3</label>
</p>
<p>
<input type="radio" name="answer" value="4" class="answer" onChange="update(this);" data-id="1">
<label>option_1_4</label>
</p>
<p>
<input type="radio" name="answer" value="5" class="answer" onChange="update(this);" data-id="1">
<label>option_1_5</label>
</p>
<div class="status2" id="status1"></div>
---------------------------------------
<!-- Second question starts (incremet i)-->
---------------------------------------
<div class="ques" id="ques2">
<input type="hidden" name="exam" value="some_exam" id="some_exam">
<input type="hidden" name="eid" value="some_examid" id="eid">
<input type="hidden" name="question" id="question2" value="question1">
<input type="hidden" name="qid" id="qid2" value="qid2">
<input type="hidden" name="sid" id="sid" value="some_session">
<p>
<input type="radio" name="answer" value="option_2_1" class="answer" onChange="update(this);" data-id="2">
<label>option_2_1</label>
</p>
<p>
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="2">
<label>option_2_2</label>
</p>
<p>
<input type="radio" name="answer" value="3" class="answer" onChange="update(this);" data-id="2">
<label>option_2_3</label>
</p>
<p>
<input type="radio" name="answer" value="4" class="answer" onChange="update(this);" data-id="2">
<label>option_2_4</label>
</p>
<p>
<input type="radio" name="answer" value="5" class="answer" onChange="update(this);" data-id="2">
<label>option_2_5</label>
</p>
<div class="status2" id="status2"></div>

Related

Radio buttons are not clickable

When executed, the radio buttons are not working, which they were when they were introduced without PHP.
Here is my code:
<td><?php echo date('l');?></td>
<?php
while ($meal_num<=4)
{
echo '<td>';
echo $row[$meal_num+2];
echo '<form action="rating.php?hostel='.$hostel.'&meal_type='.$meal_num.'" method="POST"><br><br>';
?>
<span class="radiolabel" >
<input type="radio" name="radio1" id="radio-1" value="1" />
<label for="radio-1">1</label>
<input type="radio" name="radio1" id="radio-2" value="2"/>
<label for="radio-2">2</label>
<input type="radio" name="radio1" id="radio-3" value="3"/>
<label for="radio-3">3</label>
<input type="radio" name="radio1" id="radio-4" value="4"/>
<label for="radio-4">4</label>
<input type="radio" name="radio1" id="radio-5" value="5"/>
<label for="radio-5">5</label>
</span>
<span> <input type="submit" class="submitbutton1" value="OK"></span>
</form>
</td>
<?php
$meal_num=$meal_num+1;
}?>
</tr>
</table>
Probably because your radio inputs are in a while loop, so you recreate multiple sets of radio inputs with the same set of ids.
Try attributing a different value for "id" for each loop that's executed with an increment number.
For example:
$i = 1;
while (blabla) {
echo '<input type="radio" id="radio-'.$i.'" name="radio1" />';
$i++;
}
So the radios from one loop will not interfere with the radios from another loop.
After each radio you put, you increment the number so the id will never be the same (an id should be unique in a page).

filter price by radio button in php mysql

i am working on e-commerce website and in products section i want to give a function to sort product by price range like 100 to 500 but i have no idea to connect html radio button with mysql that on click product get sort on screen.
[enter image description here][1]
This is html code for radio button
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<ul class="radio">
<li><input type="radio" name="radio" id="r1" value="radio"/>
<label for="radio1">100-500</label></li>
<li><input type="radio" name="radio" id="r2" value="radio2"/>
<label for="radio2">600-1000</label></li>
<li><input type="radio" name="radio" id="r3" value="radio"/>
<label for="radio3">1100-1500</label></li>
<li><input type="radio" name="radio" id="r4" value="radio4"/>
<label for="radio4">1600-2000</label></li>
<li><input type="radio" name="radio" id="r5" value="radio5"/>
<label for="radio5">2100-2500</label></li>
</ul>
</form>
And this php and mysql code
<?php
$results1 = mysqli_query($db,"SELECT product_code, product_name, product_img_name, price FROM products where price> 500 ORDER BY id ASC");
if($results1){
$products_item = '<ul class="products">';
while($obj = $results1->fetch_object())
{
$products_item .= <<<EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content"><h3>{$obj->product_name}</h3>
<div class="product-thumb"><img height="250px" src="../../img/{$obj->product_img_name}"></div>
<div class="product-info">
Price {$currency}{$obj->price}
<fieldset>
<label>
<span>size</span>
<select name="product_color">
<option value="Black">Medium</option>
<option value="Silver">Small</option>
</select>
</label>
<label>
<span>Quantity</span>
<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</label>
</fieldset>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<div align="center"><button type="submit" class="add_to_cart">Add</button></div>
</div></div>
</form>
</li>
EOT;
}
}
?>

Retrieving the Value of a Radio Button using PHP

Code for RadioButton:
<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">
Php Code:
<?PHP
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?
May you can assign a value:
PHP Code
<form method="POST">
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
<label for="radio1" class="css-label">Neighbourhood Only</label><br>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
<label for="radio2" class="css-label">Zipcode Only</label><br>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
<label for="radio3" class="css-label">Near Zipcode</label><br>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
<label for="radio1" class="css-label">City-Wide</label><br>
<input type="submit" value="Submit">
</form>
PHP Code:
<?php
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
A radio button is has a default value of "on", you should specify your value in your input initialization like so:
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.
Use a value attribute on your input radio.
Use form method "post" and also put value for each radio:
<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>

how to stop response time and count mouse clicks

The code below is a dynamic way of displaying each option as checkbox buttons for each question:
function ExpandOptionType($option) {
$options = explode('-', $option);
if(count($options) > 1) {
$start = array_shift($options);
$end = array_shift($options);
do {
$options[] = $start;
}while(++$start <= $end);
}
else{
$options = explode(' or ', $option);
}
echo '<p>';
foreach($options as $indivOption) {
echo '<div id="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
echo '</p>';
}
foreach ($arrQuestionId as $key=>$question) {
?>
<p><?php echo ExpandOptionType(htmlspecialchars($arrOptionType[$key])); ?></p>
<p><input type='text' class='questionIds' name='questionids' value='<?php echo htmlspecialchars($arrQuestionId[$key]); ?>' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
}
Now below I 2 text inputs which are also in the foreach loop, one for response time and other is for counting mouse click:
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
Now this is my questions:
The response time text input contains a count up timer. What I want is that if the first button checkbox is clicked in a question, the question's response timer should stop. This is so we know how long it took the user to respond answering a particular question
The mouse click text starts with 0 and what I want this text input to do is for every button checkbox that is clicked in a question, the question's mouse click text input counts up the amount of clicks so we know how many clicks on the question's options the user has compiled.
How can the above be achieved?
Below is a jsfiddle showing sample code of what it looks like for one question:
http://jsfiddle.net/zAFND/630/
UPDATE:
Source code showing multiple questions example:
QUESTION 1:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-F" value="F" />
<span>F</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='73' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
QUESTION 2:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='74' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
As you created a reference for setinterval that is good. Now you can remove the set interval on click event of first checkbox and add a click event on all checkbox to increase counter.
That will be
$(document).ready(function(){
var checkBox=$('#ck-button').find('input');
var responsetimer=//your interval function
checkbox.filter(':first').bind('click',function(e){
clearInterval(responsetimer);
});
checkbox.bind('click',function(e){
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
});
Well i am not sure about the question you want timer to be stopped at first button click or first time button clicked. If it is first time than dont use the first binding . keep clear interval in second binding itself.
checkbox.bind('click',function(e){
clearInterval(responsetimer);
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
JS fiddle :http://jsfiddle.net/zAFND/631/
for second option
http://jsfiddle.net/zAFND/638/
UPDATE
if you want this for multiple question wrap each question with a div say <div class="queWrap"></div>
Make a array refrence for your interval function so that it can be clear.
Loop to each queWrap and start timer and assign events on checkbox.
Check example for multiple question :http://jsfiddle.net/zAFND/640/

Radio Buttons & Sessions, how to get value and use as a session variable

How would i get the value of a radio button and use that in my session so for example;
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
</div>
as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?
#Xavier: Try --
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
</div>

Categories