如何根据cookie启用/禁用按钮

时间:2014-12-16 04:41:48

标签: php html cookies

我创建了名为设置Cookie 的按钮,以及删除Cookie 。现在我要求启用设置Cookie 按钮和禁用删除Cookie 按钮 Cookie未设置时,还要禁用 Cookie设置时,设置Cookie按钮启用删除Cookie按钮。以下是我的代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
setcookie("name", "John", time()+3600, "/","", 0);
setcookie("age", "36", time()+3600, "/", "",  0);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WCSST 2</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
</style>
</head>
<body>

      <?php
        if(!isset($_COOKIE["name"])) {
        echo "cookie not set";
        }else
        echo $_COOKIE["name"];
      ?>
        <button style="color: black"><b>Set Cookie</b></button>

        <button style="color: black"><b>Delete Cookie</b></button>

<!-- END PAGE SOURCE -->
</body>
</html>

任何人都可以帮助满足上述要求

1 个答案:

答案 0 :(得分:0)

尝试以下代码

 <button style="color: black" <?php if(isset($_COOKIE["name"])) { ?> Disabled <?php } ?> value='Set Cookie'>

 <button style="color: black" <?php if(!isset($_COOKIE["name"])) { ?> Disabled <?php } ?> value='Delete Cookie'>