page3.php後我回到page1.php,但我不能分配一個新的值$ _SESSION ['答案']了。我得到的錯誤未定義指數:答案爲什麼在page3.php的session_destroy()後回到page1.php,session_start()不再工作?
第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>
我只是想顯示的$ _SESSION值[ '答案']
第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>
這個警告是用於$ _POST,而不是用$ _SESSION – Jack
來詳細說明@Jack一點:回到頁面1的頁面3沒有任何輸入,因爲它是'$ _POST [ 'answer']'沒有定義。 – IzzEps
[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – miken32