简体   繁体   English

为什么这个 javascript 变量没有设置到 php 中的 cookie 中?

[英]Why does this javascript variable not get set into a cookie in php?

I have the javascript function below which sends the variable 'widgets' to a php file via an ajax call.我有下面的 javascript function,它通过 Z2705A583A97ACE2677EDA2705A4583A97ACE2679 调用将变量“小部件”发送到 php 文件。 If I then send the variable widgets back to the html document and echo it out it displays on the clientside screen correctly.如果我随后将变量小部件发送回 html 文档并将其回显,它会正确显示在客户端屏幕上。 BUT when it is sent to php I try to set it into a cookie and the cookie doesn't get set.但是当它被发送到 php 时,我尝试将其设置为 cookie 并且 cookie 没有设置。 Here is the javascript function:这是 javascript function:

function positions(){
    var widgets = '';
    var col = document.getElementById('col');
    for(i = 0; i < col.childNodes.length; i++) {
        var str1 = col.childNodes[i].className;
        if(str1 && str1.match('widget')) widgets+='&c[1]['+i+']='+col.childNodes[i].id;
    }

xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('POST', '/ajaxwidgetpositions.php', true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("widgetpositions="+widgets);
var e = document.getElementById('widget_data');
e.innerHTML += '<p><a>' +widgets + '</a></p>';
xmlhttp.send(null);
return true;
}

And here is the php:这是 php:

<?php
$widgetpositions = $_POST["widgetpositions"];
setcookie("widgetss", $widgetpositions);
?> 

For what it's worth, the javascript varibale 'widgets' has the form:对于它的价值,javascript 变量“小部件”具有以下形式:

&c[1][1]=widget_5&c[1][2]=widget_11&c[1][4]=widget_1&c[1][6]=widget_13
&c[2][2]=widget_6&c[2][4]=widget_10&c[2][6]=widget_2&c[2][8]=widget_3
&c[3][3]=widget_7&c[3][5]=widget_12&c[3][7]=widget_8

I cut out some of the function positions code to make it more readable so 'widgets'above is a bit longer than you might expect, but that is the form it takes.我删除了一些 function 位置代码以使其更具可读性,因此上面的“小部件”比您预期的要长一些,但这就是它所采用的形式。 Maybe that is too much to store in a cookie?也许这太多了,无法存储在 cookie 中?

Does anyone know why it's not getting set to a cookie?有谁知道为什么它没有设置为cookie? Please help.请帮忙。

Are you sure that the data is posted an recived on the PHP side?你确定数据是在 PHP 端发布的吗? Check this with var_dump($_POST) .var_dump($_POST)检查这个。

If it is, are you sure your session doesn't expire or something?如果是,你确定你的 session 没有过期还是什么? Since you don't have an expiration time when you set your cookie it will get wiped when the session dies.由于您在设置 cookie 时没有过期时间,因此当 session 死亡时,它将被擦除。

Try with尝试

<?php
$widgetpositions = $_POST["widgetpositions"];
setcookie("widgetss", $widgetpositions, time()+60*60);
?>

Does the cookie still not set? cookie 还没有设置吗?

this is how i set and delete cookies without having problem in IE这就是我如何设置和删除 cookies 而在 IE 中没有问题

  //Always use domain and path to control your cookies better.
  //otherwise in IE you can have problem.

  //setcookie
  $domain = "domain.com"; //without subdomain.
  $domain = "/path_of_this_file/"; //without subdomain.

  $widgetpositions = $_POST["widgetpositions"]; 
  setcookie("widgetss", $widgetpositions, time()+3600, $path, $domain, false); 

  //delete cookie
  setcookie("widgetss", '', time()-31536001, $path, $domain, false); 

?>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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