Paul Walker Paul Walker - 6 months ago 15
PHP Question

Why is it that after session_destroy() on page3.php and i go back to page1.php, session_start() is not working anymore?

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'];
$stored_answer = $_SESSION['answer'];
$_SESSION = array();
session_destroy();
?>

<body>


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

<Input type = "Submit" name="Submit3" value="TRY QUIZ AGAIN" >
<input type="hidden" name="answer" value="<?=$stored_answer?>"/>
</FORM>
</body>

Answer

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

Comments