Kyle
Kyle

Reputation: 3042

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.

Upvotes: 0

Views: 184

Answers (4)

Kyle
Kyle

Reputation: 3042

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

Upvotes: 0

brianreavis
brianreavis

Reputation: 11546

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.

Upvotes: 1

Marc B
Marc B

Reputation: 360702

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.

Upvotes: 1

Naftali
Naftali

Reputation: 146310

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')

Upvotes: 0

Related Questions