對於PHP會話來說是新手,並在傳遞會話變量時面臨問題。無法弄清楚此代碼中的問題。 第1頁無法使用PHP將會話變量傳遞到另一個頁面
<html>
<head><title>My First PHP</title></head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION = "main1.php">
<table >
<tr><td>First Name: <INPUT TYPE = "TEXT" VALUE ="" NAME = "first"></td></tr>
<tr><td>Last Name: <INPUT TYPE = "TEXT" VALUE ="" NAME = "last"></td></tr>
<tr><td><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Click Here"></td></tr></table>
</FORM>
<?php
session_start();
if(isset($_POST['Submit1'])){
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$firstname=ucwords($firstname);
$_SESSION["firstname"] = $_POST['first'];
$_SESSION["lastname"] = $_POST['last'];
echo $_SESSION["firstname"] ."-" . $_SESSION["lastname"] ;
echo "Hello, ".$firstname. " " .$lastname ."!" . "<br>";
}
?>
</body>
</html>
頁2
<html>
<head><title>My second PHP</title></head>
<body>
<?php
session_start();
$first = $_POST["first"];
$last = $_POST["last"];
$first = ucwords($first);
$last = ucwords($last);
$firstname = $_SESSION["firstname"];
echo $firstname;
?>
<FORM NAME ="form1" METHOD ="POST" ACTION = "main1.php">
<table align="center" >
<tr><td>First Name: <INPUT TYPE = "TEXT" NAME = "first" VALUE="<?php echo htmlentities($first); ?>"/></td></tr>
<tr><td>Last Name: <INPUT TYPE = "TEXT" VALUE ="<?php echo htmlentities($last); ?>" NAME = "last"></td></tr>
</table>
</FORM>
</body>
</html>
我在這裏無法訪問使用會話的$名字。有人可以幫助解決問題嗎?
未定義指數:名字是個error.Have檢查對此很多職位,但仍繼續發生錯誤。
session_start();在啓動任何html之前應該在最頂端。您不需要手動傳遞會話,它會自動進行。 – Samir