Page 1 of 1

Cookies to remember selection and add a link

Posted: Mon Nov 17, 2014 11:35 pm
by krnbrasha
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: http://omega.uta.edu/~cyjang/ctec4309/l ... ndex_2.php. Currently mine is basically the same, but when I hit advanced it does not add the extra link... what am I missing??? here's mine http://omega.uta.edu/~dxh6110/4309/cookies/

Code: Select all

$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"];

}



?>














	



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>









Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 6:43 am
by Celauran

Code: Select all

$advMenu = $menu . "<a href='#'>More Options</a>";
Where are you using $advMenu?

Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 3:58 pm
by krnbrasha
Oh, I see that I didn't use it. So... I replaced where I have <?php echo $menu ?> to $advMenu but it is always showing "more options". How do I make it so that when I select the "advanced' radio button it will show the "more options" link? and if Basic radio button is selected it does not show the "more options". This is where I'm stuck.

Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 4:13 pm
by Celauran
You can choose which to display based on the value of the cookie. Alternately, you could add the new value to $menu depending on the cookie.

Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 6:25 pm
by krnbrasha
ehhhhhm would this work at all????


//based on the rememebered selection (cookie) to define the menu
if (isset($_POST["menuType"]) && !empty($_POST["menuType"])){

$menu = $advMenu;
}else {
$menu = $menu;

}
so I tested this, and it actually shows up after I click submit. Lol, but it doesn't matter if I click basic or advanced. How to incorporate that into the code? Where it checks if advanced has been selected? O.o

Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 8:05 pm
by Celauran
Why are you only checking if it's empty? Why aren't you checking its value?

Re: Cookies to remember selection and add a link

Posted: Tue Nov 18, 2014 10:24 pm
by Christopher
I have the same question as Celauran: why empty(). In this case, the request always returns strings. And since you already did an isset() you are really checking to see if the value is not "" or "0" which is pretty strange.

I would recommend testing the menuType against literals, or using preg to validate or filter the the value.