用于记住选择并添加链接 (PHP) 的 Cookie


Cookies to remember selection and add a link (PHP)

我有作业要求我使用 cookie。所以基本上你有两个单选按钮选择,一个列为基本,一个列为高级。选择高级菜单将添加指向现有菜单的链接。如图所示:演示。目前我的基本上是一样的,但是当我点击高级时,它不会添加额外的链接......我错过了什么???我的演示

.PHP:

 $form = "
    <form action='' method='post'>'n
        Name: <input type='text' name='userName' size='10'>'n
        <input type='submit' name='submitName' value='Submit'>'n
    </form>'n'n";
    $logoutForm = "
    <form action='' method='post'> <input type='submit' name='logout' value='Log out'></form>";
    $menu = "
        | <a href='index.php'>Home</a> 'n
        | <a href='product.php'>Product</a> 'n
        | <a href='contact.php'>Contact Us</a> |'n'n
        "
         ;
 $advMenu = $menu . "<a href='#'>More Options</a>";
 $menuForm = "
 <form action='' method='post'>Menu options: 
        <input type='radio' name='menuType' value='basic'> Basic 
        <input type='radio' name='menuType' value='advanced'> Advanced 
        <input type='submit' name='selectMenu' value='Select'>
        </form>
    "
    ;
   // check to see if a userName is submitted
    if (isset($_POST["userName"]) && !empty($_POST["userName"])){
    // submission found, set up a cookie variable accordingly.  
    setcookie("user", $_POST["userName"], time() + 14400);
    // Cookies will only be available beginning next page load.  (So $_COOKIE['user'], which we just set up in the line above, is not avaible "now".) To use this cookie item "now" (this page load), we need to also assign the same value to the same $_COOKIE array item as below.
    $_COOKIE['user'] = $_POST["userName"];
    // otherwise (no UserName is submitted), check to see if the logout button is clicked.
  } else if (isset($_POST["logout"])){
    // if yes, clean up the cookie
    setcookie("user", "", time() - 3600);
    $_COOKIE['user'] = "";
 }
 //echo "<p>Cookie: {$_COOKIE['user']}</P>"; // for debugging purposes.
 // after set up or clean up the cookies, check the resulting cookie item again to compose appropriate greeting message.
 if (isset($_COOKIE["user"]) && !empty($_COOKIE["user"])){
    // there is a user name stored in the cookie.  Use it to compose a greeting
    $message = "Welcome, {$_COOKIE['user']}! $logoutForm";
 } else {
    // no user name in the cookie, output the log in form then.
    $message = $form;
}
//set cookie to remember menu selection
    if (isset($_POST["menuType"]) && !empty($_POST["menuType"])){
    setcookie("userN", $_POST["menuType"], time() +14400);
    $_COOKIE['userN'] = $_POST["menuType"];
}
if (isset($_POST["menuType"]) && !empty($_POST["menuType"])){
    $menu = $advMenu;
    }else {
    $menu = $menu;
    }
?>

.HTML:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> CTEC 4309 Class Working File: Cookie Exercise </TITLE>
<link rel="stylesheet" type="text/css" href="style.css" />
</HEAD>
<BODY>
 <hr>
<h1>Cookie Exercise</h1>
 <?php echo $message ?>
</div>
<div id="menu">
<?php echo $menu ?>
</div>
<div id="menu_option">
<?php echo $menuForm ?>
</div>
<div id="content">
<p> This is the home page</p>
</div>

用这个更改新添加的代码

if (isset($_POST["menuType"]) && !empty($_POST["menuType"]))
{
    $type=$_POST["menuType"];
    if($type=="advanced")
    {
        $menu = $advMenu;
    }
    else if($type=="basic") 
    {
        $menu = $menu;
    }
}