简体   繁体   中英

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

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" 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'>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM