【问题标题】:Cookies to remember selection and add a link (PHP)用于记住选择和添加链接的 Cookie (PHP)
【发布时间】:2014-11-19 12:29:17
【问题描述】:

我的任务要求我使用 cookie。所以基本上你有两个单选按钮选择,一个列为基本,一个列为高级。选择高级菜单会添加一个指向现有菜单的链接。如此处所示:demo。目前我的基本相同,但是当我点击高级时,它不会添加额外的链接......我错过了什么??? 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>

【问题讨论】:

  • 根据cookie设置新表单的代码在哪里??是在//based on the rememebered selection (cookie) to define the menu之后
  • 我没有那个部分,我想也许我错过了复制和粘贴我的代码......但这就是我卡住和停止的地方。
  • 在 html 代码和 php 中将 &lt;?php echo $menu ?&gt; 更改为 &lt;?php echo $tempvar?&gt; 声明 tempvar 变量;使用 if 循环,如果 cookie 包含 Advance,则使用 advmenu 初始化 tempvar,否则仅使用菜单变量初始化它
  • 我可以举个例子吗?大声笑,但谢谢你为我清理了一下......我很困惑o.O
  • 好吧,我哪儿也去不了。我明白你在说什么,只是把你说的话放到代码中我遇到了麻烦!呸。

标签: php cookies radiobuttonlist


【解决方案1】:

用这个改变你新添加的代码

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    相关资源
    最近更新 更多