deeperDATA Posted May 8, 2012 Share Posted May 8, 2012 Very new to PHP. But I'm running through some tutorials here and there and I was working on this script to calculate test scores. Could someone please assist? This is also my first post so I'm hoping it's in the right place. Thanks in advance! It is also attached if anyone needs a reference. It is reporting: Notice: Undefined variable: test_complete in C:\xampp\htdocs\lascelles\quizscriptV2.php on line 16 <? $page_title = "Calculate Test Scores"; // This runs a script when the form is submitted. if(isset($_POST['submit'])){ $quest1 = $_POST['quest1']; $quest2 = $_POST['quest2']; $quest3 = $_POST['quest3']; $quest4 = $_POST['quest4']; $quest5 = $_POST['quest5']; // Lets make sure that everything has been submitted if($quest1 == NULL OR $quest2 == NULL OR $quest3 == NULL OR $quest4 == NULL OR $quest5 == NULL) { $test_complete .='Please complete the test! <a href="javascript:history.go(-1)">Go Back</a>'; }else{ // change the quest1 to the right answer if($quest1 == "3") { $test_complete .="Question one is <span class='green'>correct</span>, well done!<br/>"; }else{ $test_complete .="Question one is <span class='red'>incorrect</span>!<br/>"; } // change the quest2 to the right answer if($quest2 == "2") { $test_complete .="Question two is <span class='green'>correct</span>, well done!<br/>"; }else{ $test_complete .="Question two is <span class='red'>incorrect</span>!<br/>"; } // change the quest3 to the right answer if($quest3 == "1") { $test_complete .="Question three is <span class='green'>correct</span>, well done!<br/>"; }else{ $test_complete .="Question three is <span class='red'>incorrect</span>!<br/>"; } // change the quest4 to the right answer if($quest4 == "3") { $test_complete .="Question four is <span class='green'>correct</span>, well done!<br/>"; }else{ $test_complete .="Question four is <span class='red'>incorrect</span>!<br/>"; } // change the quest5 to the right answer if($quest5 == "2") { $test_complete .="Question five is <span class='green'>correct</span>, well done!<br/>"; }else{ $test_complete .="Question five is <span class='red'>incorrect</span>!<br/>"; } // Now lets see if all the questions are correct, this must match the above quest settings if($quest1 == "3" & $quest2 == "2" & $quest3 == "1" & $quest4 == "3" & $quest5 == "2"){ $test_complete .="<p>Congratulations, you got all the questions correct!</p>"; }else{ // If any of the questions are not correct lets tell them $test_complete .='<p>Try again next time! <a href="javascript:history.go(-1)">Try again</a></p>'; }}} ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Calculate Test Scores</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> <!-- body,td,th { color: #000000; } .green {color:#009933;} .red {color:#CC0000;} --> </style></head> <body> <? if(!isset($_POST['submit'])){ ?> <h2>Calculate Test Scores</h2> <p>Please answer the following questions.</p> <form method="post"> <p>1. Do you like tests?<br> <input type="radio" name="quest1" value="3"> Yes<br> <input type="radio" name="quest1" value="2"> Maybe<br> <input type="radio" name="quest1" value="1"> No</p> <p>2. Do you like multiple choice?<br> <input type="radio" name="quest2" value="1"> Some times<br> <input type="radio" name="quest2" value="3"> Never<br> <input type="radio" name="quest2" value="2"> Always</p> <p>3. Do you study hard?<br> <input type="radio" name="quest3" value="2"> No<br> <input type="radio" name="quest3" value="1"> Yes<br> <input type="radio" name="quest3" value="3"> Not sure</p> <p>4. Do you like PHP?<br> <input type="radio" name="quest4" value="3"> Yes <br> <input type="radio" name="quest4" value="2"> Never <br> <input type="radio" name="quest4" value="1"> Maybe </p> <p>5. Do you enjoy programming? <br> <input type="radio" name="quest5" value="3"> Nope <br> <input type="radio" name="quest5" value="2"> Always<br> <input type="radio" name="quest5" value="1"> Never </p> <p> <input type="submit" name="submit" value="Calculate Score"> </p> </form> <? }else{ echo "<h2>Quiz Results</h2> <p>".$test_complete ."</p>"; }?> </body> </html> 18311_.php Quote Link to comment https://forums.phpfreaks.com/topic/262251-undefined-variable-new-to-php-could-someone-help-please/ Share on other sites More sharing options...
trq Posted May 8, 2012 Share Posted May 8, 2012 You are trying to append to the variable $test_complete, however, the first time you try and do this, it wouldn't have been defined yet. You can fix this by defining it before you try to append to it. eg; $test_complete = ''; Quote Link to comment https://forums.phpfreaks.com/topic/262251-undefined-variable-new-to-php-could-someone-help-please/#findComment-1343960 Share on other sites More sharing options...
deeperDATA Posted May 8, 2012 Author Share Posted May 8, 2012 Thanks that worked like a charm! I inserted on line 3 and what do you know? Instant fix. Thank you Thorpe! Quote Link to comment https://forums.phpfreaks.com/topic/262251-undefined-variable-new-to-php-could-someone-help-please/#findComment-1343963 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.