Cookies to remember selection and add a link (PHP)

284 views Asked by At

I have assignment that requires me to use cookies. So basically you have two radio button choices, one listed as basic and one listed as advanced. Choosing the advanced menu adds a link to the existing menu. As shown here: demo. Currently mine is basically the same, but when I hit advanced it does not add the extra link... what am I missing??? my demo

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>
1

There are 1 answers

0
Atul Nar On BEST ANSWER

Change your newly added code with this

if (isset($_POST["menuType"]) && !empty($_POST["menuType"]))
{

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