Is there a way to modify my database-powered quiz so that, if a user doesn't answer one or more questions, clicking the Submit button will trigger an alert? In other words, I want to force users to answer all the questions, even if they just guess. The scoring mechanism doesn't work correctly unless all the questions have a response.
Note that some questions (the last one in this example) may have multiple correct answers. I think all that matters is that at least one checkbox is selected.
<form action="" method="post" id="quiz">
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9[]" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9[]" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9[]" id="q9-C" value="C" style="display: none;"> C. contracting</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9[]" id="q9-D" value="D" style="display: none;"> D. becoming bipolar</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label>
</div>
</li>
</ol>
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="54f3f5438292e" />
<input type="submit" value="Submit Quiz" />
</form>
Please check the complete solution to your question as below:
<form action="" method="post" id="quiz" onsubmit="return formValidate();">
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9[]" id="q9-A" value="A"> A. disappearing</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9[]" id="q9-B" value="B"> B. expanding</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9[]" id="q9-C" value="C"> C. contracting</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9[]" id="q9-D" value="D"> D. becoming bipolar</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label>
</div>
</li>
</ol>
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="54f3f5438292e" />
<input type="submit" value="Submit Quiz" />
</form>
<script type="text/javascript">
function formValidate(){
var qno = 2; // Total Questions, say. in your example it's 2 (9 and 10)
var i = 9; // Question No to start with, say. in your example it's 9
var qcount = (i+qno)-1;
for(i; i <= qcount; i++){
qid = 'q'+i+'[]';
var qidElement = document.getElementsByName(qid);
var checkcount = 0;
for(var j=0; j < qidElement.length; ++j){
if(qidElement[j].checked){
++checkcount;
}
}
if(checkcount == 0){
alert("Please select at least one Answer for Question no. "+i);
return false;
}
}
document.getElementById('quiz').submit();
}
</script>
Input values for qno & i in javascript function as per your requirement, as guided by me in the relevant comments beside them.
Let me know if there is any issue. Thanks! :)
If you need to force users to fill all fields you have to use one off those validation methods :
1- Client side: via java script , but user can bypass it by disable java script "not recommended".
2- Server side: user can't disable it , and the code related to your programming language .
3- Mix type: Via Ajax and that is the best choice
Example
A-Check request in server side and return results in JSON type .
B-Process returned data via JavaScript and display results in the form
Check this links will help you so much:
http://www.formget.com/form-validation-using-ajax/
http://www.w3schools.com/php/php_form_validation.asp
Related
I created a simple quiz program using only php and there are 4 different question and radio buttons and when i click any of option then print the value of radio buttons like A or B or C or D I created 4 php variable, assigned the values by $_POST method and i want to print these values by echo. But Something is not being printed Please Help..
1. <style type="text/css"> body { background-color: grey; }
#computer , #politics { cursor: pointer; background:
linear-gradient(to right,blue,#bfbfe0); color: #fff; padding:
0px 0px 0px 5px; border-radius: 5px; width: 100%; }
.computer_qsnans , .politics_qsnans { display: ; }
</style> </head> <body>
<div class="main"> <form method="GET" action="index.php"> <div
id="computer"> <p>
<h1>
Technology
</h1> </p> </div>
<!-- Tecnical --> <div class="computer_qsnans"> <p>
<h4>
1.What is the full form of HTML
<div>
<input type="radio" name="tech_question_1" id="" value="A">
A) High Transparent Markup Laoyality
<input type="radio" name="tech_question_1" id="" value="B">
B) Hyper Text Markup Language
<input type="radio" name="tech_question_1" id="" value="C">
C) Hyper Text Mechanical Language
<input type="radio" name="tech_question_1" id="" value="D">
D) Hidro Terminate Markup Language
</div>
</h4> </p>
<p>
<h4>
2.What is upcoming android version
<div>
<input type="radio" name="tech_question_2" id="" value="A">
A) Pie
<input type="radio" name="tech_question_2" id="" value="B">
B) N
<input type="radio" name="tech_question_2" id="" value="C">
C) L
<input type="radio" name="tech_question_2" id="" value="D">
D) Q
</div>
</h4> </p> </div> <!-- Political -->
<div id="politics"> <p>
<h1>
Political
</h1> </p> </div>
<div class="politics_qsnans"> <p>
<h4>
1.The present Lok Sabha is the
<div>
<input type="radio" name="poli_question_1" id="" value="A">
A) 14th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="B">
B) 15th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="C">
C) 16th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="D">
D) 17th Lok Sabha
</div>
</h4> </p>
<p>
<h4>
2.The minimum age to qualify for election to the Lok Sabha is
<div>
<input type="radio" name="poli_question_2" id="" value="A">
A) 25 years
<input type="radio" name="poli_question_2" id="" value="B">
B) 21 years
<input type="radio" name="poli_question_2" id="" value="C">
C) 18 years
<input type="radio" name="poli_question_2" id="" value="D">
D) 35 years
</div>
</h4> </p> </div>
<input type="submit" name="Submit" name="sbt" id="sbmt"> </form> </div><!--Main div end-->
</body> </html>
<?php if(isset($_GET['sbt'])) {
$answer1_for_qsn1 = $_GET['tech_question_1']; $answer2_for_qsn2 =
$_GET ['tech_question_2']; $answer3_for_qsn3 = $_GET
['poli_question_1']; $answer4_for_qsn4 = $_GET ['poli_question_2'];
echo $answer1_for_qsn1 ; echo $answer2_for_qsn2 ; echo
$answer3_for_qsn3 ; echo $answer4_for_qsn4 ;
}
?>
You are using $_GET, not $_POST
You are declaring name for button twice, so the first name will be the one that is sent with GET.
Fix:
<input type="submit" name="Submit" id="sbmt">
<?php if(isset($_GET['Submit'])) {
This worked for me when copying all your code and testing it.
EDIT: also send form to self since the if statement is in the same document as form.
<form method="GET" action="">
Make your form method POST and get your data with $_POST["tech_question_1"] instead of $_GET $_POST["sbt"] will not exist
<input type="submit" name="Submit" name="sbt" id="sbmt">
You have declared name twice. Thats why its not working
So I am using a form multiple radio buttons, The purpose is for the user to rate different parts of the site with 10 questions.
The user presses "start rating" button and the 1st question appears, then the 2nd etc.
When he finishes all of the 10 questions a submit button appears.
The problem is that, when I'm trying to retrieve these values on PHP, I get nothing, its like the radio buttons hasn't been pressed at all.
Newbie in HTML.
HTML
<form name="f1" action="rate.php" method="POST">
<!--1η ερώτηση-->
<div class="question_div" id="question_div1">
<center><p class="questiontitle"><b>Ερώτηση 1 από 10</b></p></center>
<p>Νομίζω ότι θα ήθελα να χρησιμοποιώ αυτά τα παιχνίδια συχνά.</p>
<div class="answer_div">
<div class="answer_options">
<label class="opt_label">
<input type="radio" name="input1" id="input1_1" value="1" class="option">
<span class="opt_span"><b>1</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_2" value="2" class="option">
<span class="opt_span"><b>2</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_3" value="3" class="option">
<span class="opt_span"><b>3</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_4" value="4" class="option">
<span class="opt_span"><b>4</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_5" value="5" class="option">
<span class="opt_span"><b>5</b></span>
</label>
</div>
<div class="answer-options">
<div align="left" style="width:20%;
float:left;
margin-right:30px;">Διαφωνώ απολύτως</div>
<div align="right" style="width:20%;
float:right;
margin-left:30px;">Συμφωνώ απολύτως</div>
</div>
</div>
<button class="nextbtn" type="button" id="btn1" onclick="(function(){
document.getElementById('question_div1').style.display = 'none';
document.getElementById('question_div2').style.display = 'block';
return false;
})();return false;">Επόμενη Ερώτηση</button>
</div>
this is the start of the form and the 1st question, the other questions follow the same architecture.
PHP:
if(!empty($_POST["f1"])){
$q1 = $_POST['input1'];
$q2 = $_POST['input2'];
$q3 = $_POST['input3'];
$q4 = $_POST['input4'];
$q5 = $_POST['input5'];
$q6 = $_POST['input6'];
$q7 = $_POST['input7'];
$q8 = $_POST['input8'];
$q9 = $_POST['input9'];
$q10 = $_POST['input10'];
}
It's because you have the same <input type="radio" name="input1" id="input1_1" value="1" class="option"> with the same name value. Try removing the name a attribute in your <form> and do one radio first and in your php file do this:
HTML
<form method="POST" action='rate.php'>
<div> question </div>
<input type="radio" name="input1">
<input type="submit">
</form>
rate.php
<?php
if(isset($_POST['input1']) {
echo $_POST['input1'];
//do your code here
}
I have a webpage with multiple questions which is being loaded from my database. Each question has a radio-box because users have to choose an answer from the value of 1 to 5.
Every time I hit the hit the submit button, it goes to a page with the link address showing the users answers (i.e. submit.php?12=1&14=2&15=3&16=4&17=5)
The first value representing the questionID and the second value is the score for that specific questionID.
How do I insert those in my database? I want to insert the questionID and the answer to a table which has a question field and an answer field.
<form action="../process/submit.php" >
<?php
$pin=$_SESSION['pin'];
$question = mysql_query("SELECT question.question, question.questionID FROM question");
$index=0;
while($row=mysql_fetch_array($question)){
?>
<div id="qpanel"><?=$row['question']?>
<div style="float:right;">
<div id="ratingbox" style="vertical-align:middle;">
<label>
<input type="radio" name="<?=$row['questionID']?>" value="1" >
<div id="img1" title="Poor">1</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="2">
<div id="img1" title="Average">2</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="3" >
<div id="img1" title="Good">3</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="4" >
<div id="img1" title="Very Good">4</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="5" >
<div id="img1" title="Excellent">5</div>
</label>
</div>
</div>
</form>
Use name field in this format:
<label>
<input type="radio" name="questionID[<?=$row['questionID']?>][]" value="5" >
<div id="img1" title="Excellent">5</div>
</label>
You will return in POST array with questionID and selected elements.
I am a newbie in PHP. I have a form like below:
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-ge-1" class="radio-custom" name="radio-ge" type="radio" value="1" checked>
<label for="radio-ge-1" class="radio-custom-label tooltips">1 <span>Impossible to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-2" class="radio-custom" name="radio-ge" value="2" type="radio">
<label for="radio-ge-2" class="radio-custom-label tooltips">2 <span>You have to work hard to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-3" class="radio-custom" name="radio-ge" value="3" type="radio">
<label for="radio-ge-3" class="radio-custom-label tooltips">3 <span>The usual, just like any other class</span></label>
</div>
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-sl-1" class="radio-custom" name="radio-sl" value="1" type="radio" checked>
<label for="radio-sl-1" class="radio-custom-label tooltips">1 <span>Unbearable pressure</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-2" class="radio-custom" name="radio-sl" value="2" type="radio">
<label for="radio-sl-2" class="radio-custom-label tooltips">2 <span>High pressure, But doable</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-3" class="radio-custom" name="radio-sl" value="3" type="radio">
<label for="radio-sl-3" class="radio-custom-label tooltips">3 <span>Fair, just like any other class</span></label>
</div>
I'm trying to save both radio buttons value in PHP by using the code:
$g_Easiness = $_POST['radio-ge'];
$s_Load = $_POST['radio-sl'];
But in this case, I'me only getting the first button's value. Second button's value = on.
I have spend more than two hours to figure it out. But cannot resolve it. Can anyone please help me to solve the problem?
Please use below code for radio buttons
if input type="radio" then it should give value="" otherwise in value it will show "on"
e.g.
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked>
output = [radio-nns] => on
instead put ( you can see value="1")
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked value="1">
output = [radio-nns] =>1
I have a JavaScript working on some PHP code as follows:
$(function(){
$("#hosted_prev_no").change(function(){
$("#attend").show();
$("#eligible").hide();
});
$("#hosted_prev_yes").change(function(){
$("#attend").hide();
$("#eligible").show();
});
$("#attend_prev_yes").change(function(){
$("#eligible").show();
});
$("#attend_prev_yes").change(function(){
$("#eligible").hide();
});
The php code is as follows:
<div class="full row column">
<label id="hosted_prev_label" for="hosted_prev" class="required">Have you hosted a Road Scholar program for Stetson University in the past?</label>
<label id="hosted_prev_yes_label" for="hosted_prev_yes">Yes</label>
<input id="hosted_prev_yes" name="hosted_prev" type="radio" value="Yes" />
<label id="hosted_prev_no_label" for="hosted_prev_no">No</label>
<input id="hosted_prev_no" name="hosted_prev" type="radio" value="No" /><br />
<div id="attend" class="hidden">
<label id="atend_prev_label" for="atend_prev" class="required">Have you attended at least one Stetson Road Scholar program in the past?</label>
<label id="attend_prev_yes_label" for="attend_prev_yes">Yes</label>
<input id="attend_prev_yes" name="attend_prev" type="radio" value="Yes" />
<label id="attend_prev_no_label" for="hosted_prev_no">No</label>
<input id="attend_prev_no" name="attend_prev" type="radio" value="No" /><br />
</div>
<div id="ineligible" class="hidden">
<p>All hosts are required to attend at least one Stetson University Road Scholar program before they may host p program. You may apply again once you have attended a Stetson University Road Scholar program. Thank you for your interest.</p>
</div>
</div>
<div id="eligible" class="full row column hidden">
<h2>Cohost Information</h2>
<label id="is_cohost_label" for="is_cohost" class="required">Will you be hosting with anyone?</label>
<label id="is_cohost_yes_label" for="is_cohost_yes">Yes</label>
<input id="is_cohost_yes" name="is_cohost" type="radio" value="Yes" />
<label id="is_cohost_no_label" for="is_cohost_no">No</label>
<input id="is_cohost_no" name="is_cohost" type="radio" value="No" /><br />
</div>
The problem is that the second radios not displaying the "eligible" div. How do you know which change function has precedence here?
You are binding the same element twice:
$("#attend_prev_yes").change(function(){
$("#eligible").show();
});
$("#attend_prev_yes").change(function(){
$("#eligible").hide();
});
I think what you need is:
$("#attend_prev_yes").change(function(){
$("#eligible").show();
});
$("#attend_prev_no").change(function(){
$("#eligible").hide();
});