DanielHardy Posted November 19, 2008 Share Posted November 19, 2008 Hi, I have the following code for a test. The user inputs their answer, and depending on what they put, a message is displayed. The code works fine when this is done. However, when the page is first loaded I get a series of error messages like : Notice: Undefined index: que2 in /home/stud/1/0607197/public_html/WyrleyJuniorsQuiz.php on line 161 Here is my code: <?php if ( isset($_POST["que2"])) { $question1=$_POST["que2"]; } else { $question1=""; } $question2 = $_POST["que2"]; if ($_POST["que2"] == "62") { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"green\">That is Correct!</b></font>"; } elseif ($_POST["que2"] == "") { echo ""; } else { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"red\"> Incorrect, try again!</b></font>"; } ?> Any ideas how to stop this? Thanks Dan Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/ Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 <?php if ( isset($_POST["que2"])) { $question1=$_POST["que2"]; $question2 = $_POST["que2"]; // not sure why you are setting this to same thing as above and not using it anywhere...but ok. if ($_POST["que2"] == "62") { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"green\">That is Correct!</b></font>"; }elseif ($_POST["que2"] == "") { echo ""; }else { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"red\"> Incorrect, try again!</b></font>"; } } else { $question1=""; } ?> Basically if the $_POST["que2"] isset you want to run that code inside the if, by putting the code I put inside the if outside of it you defeated the purpose of that check. EDIT: decided to post some cleaner code: <?php if ( isset($_POST["que2"])) { //$question1=$_POST["que2"]; not needed twice for the same variable. $question2 = $_POST["que2"]; // not sure why you are setting this to same thing as above and not using it anywhere...but ok. }else { $question2=""; } // use the variable you defined here. if ($question2 == "62") { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"green\">That is Correct!</b></font>"; }elseif ($question2 != "") { echo "<b>You put:$question2</b>"; echo "<p><b><font color=\"red\"> Incorrect, try again!</b></font>"; } ?> Removed the last echo as it is not needed cause it does not print anything out. Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693486 Share on other sites More sharing options...
kenrbnsn Posted November 19, 2008 Share Posted November 19, 2008 Here's another version of the code <?php <?php $question2 = (isset($_POST['que2']))?$_POST['que2']:''; $color = ''; $response = ''; switch ($question2) { case '62': $color = 'green'; $response = 'That is Correct'; break; case '': break; default: $color = 'red'; $response = 'Incorrect, try again'; } if ($color == '') echo ''; else echo '<div style="font-weight:bold">You put: ' . $question2 . '<p style="color:' . $color . '">' . $response . '!</p></div>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693491 Share on other sites More sharing options...
DanielHardy Posted November 19, 2008 Author Share Posted November 19, 2008 Solved it. Thanks a lot and don't i feel stupid lol. Yeah I know there is no value in assigning the same value to different variables, this as a simple copy and paste error. Thanks for your help. I did have it working perfectly before but my server administrators decided to disable all global commands!!! Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693492 Share on other sites More sharing options...
kenrbnsn Posted November 19, 2008 Share Posted November 19, 2008 but my server administrators decided to disable all global commands!!! Do you mean they finally disabled register_globals? If so, that's good and should have been done 3 or 4 years ago. Ken Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693496 Share on other sites More sharing options...
DanielHardy Posted November 19, 2008 Author Share Posted November 19, 2008 Yeah they did. I'm a newbie if you haven't realised! What are the security problems with register_globals? Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693503 Share on other sites More sharing options...
kenrbnsn Posted November 19, 2008 Share Posted November 19, 2008 Read this manual page Ken Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693506 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 http://phpsec.org/projects/guide/1.html Google "Register_globals security risk" for more information. Quote Link to comment https://forums.phpfreaks.com/topic/133339-should-be-simple-if-someone-can-help-please/#findComment-693508 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.