开发者

made a cookie in php, now trying to update it in javascript?

开发者 https://www.devze.com 2023-02-16 20:01 出处:网络
echo\"<script type=\\\"text/javascript\\\">\"; echo \"function del_cookie()\"; echo \"{\"; echo \"document.cookie = logged_amount + \'=; expires=Thu, 01-Jan-70 00:00:01 GMT;\';\";
echo  "<script type=\"text/javascript\">";
echo "function del_cookie()";
echo "{";
echo "document.cookie = logged_amount + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';";
echo "}";

echo "</script>";

echo "<form action=logout.php name=Logout>";
echo "<input type=submit name=submit value=\"Log Out\"> | <a href='./archive.php'>View History</a> | <a href='#' onclick='del_cookie'>Mark as read</a><form>";


setcookie("logged_amount", $x, time() + 60 * 60 * 24 * 30);

When I try to click "Mark as read"开发者_如何学JAVA it doesn't really work. Am I missing something? Also if I can I'd really like to update the cookie with the $x value.


document.cookie =  logged_amount +
                  ^             ^

You didn't surround the logged_amount with quotes, so Javascript's seeing it as an (undefined) variable. So you're trying to set a cookie with a blank name. Try

document.cookie = 'logged_amount' + ...

instead.


Note: setcookie() in PHP doesn't work after the headers are sent. In other words, you need to place setcookie() before you echo out anything.


you can use the jquery plugin: https://github.com/carhartl/jquery-cookie

to read cookies in jquery use the:

$.cookie('cookie_name')

and to set it use:

$.cookie('cookie_name','cookie_val')


I gave up with javascript.

if (isset($_GET['markread'])) {
    if ($_GET['markread'] == 1) {
        setcookie("logged_amount", $x, time() + 60 * 60 * 24 * 30);
        echo "<meta http-equiv='refresh' content='0;url=view.php'>";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消