-1

After page3.php i go back to page1.php but i cant assign a new value to $_SESSION['answer'] anymore. i'm getting the error Undefined Index: answer

PAGE 1:

<?php

session_start();

$true_status = 'unchecked';
$false_status = 'unchecked';

if (isset($_POST['Submit1'])) {

 $_SESSION['answer'] = $_POST['answer'];

$selected_radio = $_POST['answer'];


if ($selected_radio == 'true') {
$true_status = 'checked';

                }
else if ($selected_radio == 'false') {
$false_status = 'checked';



                    }
    }                   



?>

<body>

<FORM name ="form1" method ="post" action = 'page2.php'>

<h2>Q1. True or False?</h2>
<Input type = 'Radio' Name ='answer' value= 'true'
<?php echo $true_status; ?>
>TRUE

<Input type = 'Radio' Name ='answer' value= 'false' 
<?php echo $false_status; ?>
>FALSE

<p>
<Input type = "Submit" Name = "Submit1" VALUE = "Next Page">


</FORM>
</body>

I'm just trying to display the value of $_SESSION['answer']

PAGE 2:

<?php
session_start();

echo $_SESSION['answer'];

?>

<body>
<FORM name ="form3" method ="post" action ="page3.php">
<Input type = "Submit" name="Submit2" value="Next Page" >
</FORM>

</body>

PAGE 3:

<?php
session_start();

echo $_SESSION['answer'];

$_SESSION = array();
session_destroy();
?>

<body>


<FORM name ="form2" method ="post" action ="page1.php">

<Input type = "Submit" name="Submit3" value="TRY QUIZ AGAIN" >

</FORM>
</body>
  • 1
    that warning is for $_POST, not for $_SESSION – Jack May 16 '16 at 22:02
  • to elaborate on @Jack a little: page 3 which is posting back to page 1 does not have any input withe the `name` answer` so it's `$_POST['answer']` is not defined. – IzzEps May 16 '16 at 22:29
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – miken32 May 16 '16 at 23:03

3 Answers3

1

because you code in page3.php have Submit type with name Submit1 when code reach in this line

if (isset($_POST['Submit1'])) {
 $_SESSION['answer'] = $_POST['answer'];
}

its become true but you dont have any element have name like answer thats way its error showing for fixing this problem in page3.php change name of Submit1 to another name like `firstpage'.

Edit

since you in page1 pass form to page2 you have to add this line on top in page2

$_SESSION['answer'] = $_POST['answer'];

enter image description here

Hosseini
  • 547
  • 3
  • 15
1

As the others have mentioned in their comments and answers:

The warning you get is because the $_POST doesn't contain the 'answer' key, because in page3.php the name of the input is "Submit1". The $_POST global variable is an array that contains values with the keys taken from the name property of the inputs.

benelori
  • 78
  • 1
  • 8
1

In short page 3 which is posting back to page 1 does not have any input with the name answer so it's $_POST['answer'] is not defined.

If you would like to carry over the "answer" from page 3 back to page 1, one option would be to use a hidden input, like so:

  1. (copy and paste this code onto the line before $_SESSION = array(); on page 3) $stored_answer = $_SESSION['answer'];
  2. (and copy and paste this code onto the line before </FORM> also on page 3) <input type="hidden" name="answer" value="<?=$stored_answer?>"/>.

This will preserve your "answer" as you move from page3 back to page 1.

IzzEps
  • 582
  • 6
  • 20
  • why store it when he want new value in `page1`? – Hosseini May 16 '16 at 22:43
  • hence my preface "**If** you would like to carry...". I could easily think of a scenario where he would want the user to be able to see their previous/incorrect answer when they are re-trying... – IzzEps May 16 '16 at 22:45
  • **again** still its handle this stuff before going to page he gonna create get new value and incorrect one stay in `page3` – Hosseini May 16 '16 at 22:51
  • i tried this but the error now shows in page1, before its in page2 and 3 – Paul Walker May 16 '16 at 23:02
  • @PaulWalker - can you post your updated code? – IzzEps May 16 '16 at 23:04
  • pls check the edited version on top, i tried it again. there is no error this time but it's not displaying any value on page2 and 3. – Paul Walker May 16 '16 at 23:19
  • that's because on page 1 you are not only checking for `$_POST['answer']` but you are also checking to see if `isset($_POST['Submit1'])` before you store the answer in the session. I'm not 100% clear on what you're trying to do here but just in the interest of confirming that that is indeed the issue - just change that line on page 1 to `if (isset($_POST['answer']))`... makes sense? – IzzEps May 16 '16 at 23:28
  • tnx. i add $_SESSION['answer'] = $_POST['answer']; on page2 like what @Hosseini said and its working now. i'm just trying to learn about session_start(), $_SESSION. – Paul Walker May 16 '16 at 23:54