Со вчерашнего дня я пытаюсь решить эту проблему, я знаю, что существует множество подобных учебных пособий, но они как-то не работают для моей ситуации.
То, что я пытаюсь сделать, это установить cookie, когда пользователь нажимает на любой продукт (он должен хранить product_id). Так что этот продукт будет добавлен в график.
Я проверяю его на консоли javascript в Chrome, но возникает странная ошибка «Uncaught SyntaxError: Unnexpected token}», и он говорит, что сохраненных 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> ";
?>
И этот код JavaScript находится в начале страницы:
<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>
заранее спасибо