Cookies, чтобы запомнить выбор и добавить ссылку (PHP)

0

У меня есть задание, которое требует, чтобы я использовал файлы cookie. Таким образом, в основном у вас есть два выбора переключателя, один из которых указан как базовый, а один - как расширенный. Выбор расширенного меню добавляет ссылку на существующее меню. Как показано здесь: 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>
  • 0
    где твой код для установки новой формы в соответствии с cookie? это после //based on the rememebered selection (cookie) to define the menu
  • 0
    У меня нет этой части, я подумал, может быть, я пропустил копирование и вставку своего кода ... но я просто застрял и остановился.
Показать ещё 4 комментария
Теги:
cookies
radiobuttonlist

1 ответ

1
Лучший ответ

Измените свой новый код на этот

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

    $type=$_POST["menuType"];
    if($type=="advanced")
    {
        $menu = $advMenu;
    }
    else if($type=="basic") 
    {
        $menu = $menu;
    }
}
  • 0
    о человек, я скучал много. Большое спасибо.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню