Jump to content

Should be simple if someone can Help????!!! Please!!!!???


DanielHardy

Recommended Posts

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

<?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.

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

 

 

 

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!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.