I am looking since yesterday to fix this problem, I know there are plenty of similar tutorials but somehow they didnt work for my situation.
What I am trying to do is setting the cookie whenever the user click on any product(It should store product_id). So this product is going to be added to chart.
I check it on javascript console of the chrome but there is a strange error “Uncaught SyntaxError: Unexpected token }” and it says there isnt any stored cookie.
echo "<div id='products'>";
for($i=0;$i<$num;$i++)
{
$ftc = mysql_result($result, $i, "product_image");
$ftc2 = mysql_result($result, $i, "product_id");
$ftc3 = mysql_result($result, $i, "product_name");
$ftc4 = mysql_result($result, $i, "product_preis");
$ftc5 = mysql_result($result, $i, "product_old_preis");
echo "<a href='details.php?product=".$ftc2."'>
<div class='products_list'>
<img width='217px' height='323px' src='".$ftc."'/>
<div class='alt_yazi2'><b>".$ftc3 ."  
<font style='float:right;'>".$ftc4."TL  <b style='text-decoration:line-through; float:right;'>".$ftc5."TL </b></font></b></a>
</br>
<div id='sepet'><a onclick='setCookie('ccname', '".$ftc2."', 'expiry');' href='#'>
<img width='35px' height='25px' src='images/cart_icon.png'/>
</a>
</div>
</div></div> ";
}
echo "</div> ";
?>
And this javascript code is on the head of the page:
<script type="text/javascript" >
// JavaScript Document
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
</script>
thanks in advance
The error is probably because of this function
toGMTString(). If you had usedscript type="javascript", thetoGMTString()would have worked. Since you’ve usedscript type="text/javascript", you do not need to convert to GMT format, browsers nowadays do it for you.I suggest you just remove the conversion to GMT and give a try.
Regards