nclocl Posted August 21, 2011 Share Posted August 21, 2011 I have the following code that I am trying to validate: <div id="content"> <br /> <form onsubmit="return validate();" method="post" action="submitquiz.php?id=<? $_GET['question'] ?>"> <br /> <? session_start(); if (!session_is_registered(email)) { header("location:login.html"); } $con = mysql_connect("***************************"); if (!$con) { die('Connection Failed' . mysql_error()); } mysql_select_db("****************", $con); ?> <h3><? echo $_GET['question'] ?></h3> <fieldset><legend>Question 1</legend> <? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '1'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $question1 = $essay_data['QUESTION']; $answer1 = $essay_data['ANSWER1']; $answer2 = $essay_data['ANSWER2']; $answer3 = $essay_data['ANSWER3']; $answer4 = $essay_data['ANSWER4']; $answer5 = $essay_data['ANSWER5']; echo "$question1"; echo "<br><br>"; echo "Answers:"; echo "<br><br>"; echo "A: "; echo "$answer1"; echo "<br><br>"; echo "B: "; echo "$answer2"; echo "<br><br>"; echo "C: "; echo "$answer3"; echo "<br><br>"; echo "D: "; echo "$answer4"; echo "<br><br>"; } ?> <div> <select name="answer1"> <option value="">Select an answer:</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> </div> </fieldset> <fieldset><legend>Question 2</legend> <? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '2'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $question1 = $essay_data['QUESTION']; $answer1 = $essay_data['ANSWER1']; $answer2 = $essay_data['ANSWER2']; $answer3 = $essay_data['ANSWER3']; $answer4 = $essay_data['ANSWER4']; $answer5 = $essay_data['ANSWER5']; echo "$question1"; echo "<br><br>"; echo "Answers:"; echo "<br><br>"; echo "A: "; echo "$answer1"; echo "<br><br>"; echo "B: "; echo "$answer2"; echo "<br><br>"; echo "C: "; echo "$answer3"; echo "<br><br>"; echo "D: "; echo "$answer4"; echo "<br><br>"; } ?> <div> <select name="answer2"> <option value="">Select an answer:</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> </div> </fieldset> <fieldset><legend>Question 3</legend> <? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '3'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $question1 = $essay_data['QUESTION']; $answer1 = $essay_data['ANSWER1']; $answer2 = $essay_data['ANSWER2']; $answer3 = $essay_data['ANSWER3']; $answer4 = $essay_data['ANSWER4']; $answer5 = $essay_data['ANSWER5']; echo "$question1"; echo "<br><br>"; echo "Answers:"; echo "<br><br>"; echo "A: "; echo "$answer1"; echo "<br><br>"; echo "B: "; echo "$answer2"; echo "<br><br>"; echo "C: "; echo "$answer3"; echo "<br><br>"; echo "D: "; echo "$answer4"; echo "<br><br>"; } ?> <div> <select name="answer3"> <option value="">Select an answer:</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> </div> </fieldset> <fieldset><legend>Question 4</legend> <? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '4'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $question1 = $essay_data['QUESTION']; $answer1 = $essay_data['ANSWER1']; $answer2 = $essay_data['ANSWER2']; $answer3 = $essay_data['ANSWER3']; $answer4 = $essay_data['ANSWER4']; $answer5 = $essay_data['ANSWER5']; echo "$question1"; echo "<br><br>"; echo "Answers:"; echo "<br><br>"; echo "A: "; echo "$answer1"; echo "<br><br>"; echo "B: "; echo "$answer2"; echo "<br><br>"; echo "C: "; echo "$answer3"; echo "<br><br>"; echo "D: "; echo "$answer4"; echo "<br><br>"; } ?> <div> <select name="answer4"> <option value="">Select an answer:</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> </div> </fieldset> <fieldset><legend>Question 5</legend> <? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '5'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $question1 = $essay_data['QUESTION']; $answer1 = $essay_data['ANSWER1']; $answer2 = $essay_data['ANSWER2']; $answer3 = $essay_data['ANSWER3']; $answer4 = $essay_data['ANSWER4']; $answer5 = $essay_data['ANSWER5']; echo "$question1"; echo "<br><br>"; echo "Answers:"; echo "<br><br>"; echo "A: "; echo "$answer1"; echo "<br><br>"; echo "B: "; echo "$answer2"; echo "<br><br>"; echo "C: "; echo "$answer3"; echo "<br><br>"; echo "D: "; echo "$answer4"; echo "<br><br>"; } ?> <div> <select name="answer5"> <option value="">Select an answer:</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> </div> </fieldset> <fieldset> <legend>Submit your Answers</legend> <div> <input type="hidden" name="question" value="<?= $_GET['question'] ?>" /> </div> <div> <input type="hidden" name="correct1" value="<? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '1'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $correct = $essay_data['CORRECT']; echo "$correct"; } ?>" /> </div> <div> <input type="hidden" name="correct2" value="<? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '2'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $correct = $essay_data['CORRECT']; echo "$correct"; } ?>" /> </div> <div> <input type="hidden" name="correct3" value="<? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '3'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $correct = $essay_data['CORRECT']; echo "$correct"; } ?>" /> </div> <div> <input type="hidden" name="correct4" value="<? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '4'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $correct = $essay_data['CORRECT']; echo "$correct"; } ?>" /> </div> <div> <input type="hidden" name="correct5" value="<? $question = $_GET['question']; $result = mysql_query("SELECT * FROM QUESTIONS WHERE QUIZ_NAME = '$question' AND QUESTION_NO = '5'") or die(mysql_error()); while ($essay_data = mysql_fetch_array($result)) { $correct = $essay_data['CORRECT']; echo "$correct"; } ?>" /> </div> <div> <input type="submit" value="Submit" /> </div> </fieldset> </form> </div> And I tried to use the following javascript to validate part of it: function validate() { // validate form. var warningMessage = "" // presume no errors so far. //Q1 warning1 = "" if ( document.forms[0].answer1.selectedIndex == 0 ) { warning1 = "Q1. Please select an answer."+ "\n"; // if nothing selected, an error is given. document.getElementById("answer1").style.background = 'yellow'; } else{ warning1 = ""; document.getElementById("answer1").style.background = ''; } warningMessage = warning+warning1+warning2+warning3+warning4; // combine error messaged. if (warningMessage == "") { alert("Your score has been submitted.") return true // display score and submit if no errors are present. } else { alert(warningMessage) //if there are errors, display them and do not submit. return false } } Any idea why it just gets submitted when I leave the selectbox value at 0? Quote Link to comment https://forums.phpfreaks.com/topic/245369-validation-help/ Share on other sites More sharing options...
Omirion Posted August 21, 2011 Share Posted August 21, 2011 warning+warning1+warning2+warning3+warning4 Most of these are undefined and i suspect the are breaking your script. If you don't insist on writing your own validation script i advice you to try this: http://docs.jquery.com/Plugins/validation Quote Link to comment https://forums.phpfreaks.com/topic/245369-validation-help/#findComment-1260254 Share on other sites More sharing options...
nclocl Posted August 21, 2011 Author Share Posted August 21, 2011 I tried removing that but it didn't help. I'll have a look at that plugin but it's only this page I need validating so hardly seems worth it. Quote Link to comment https://forums.phpfreaks.com/topic/245369-validation-help/#findComment-1260309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.