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

为什么这个javascript变量没有被设置到php中的cookie中?,php,javascript,cookies,Php,Javascript,Cookies,下面是javascript函数,它通过ajax调用将变量“widgets”发送到php文件。如果我随后将变量小部件发送回html文档并将其回显,它将正确地显示在客户端屏幕上。但是当它被发送到php时,我尝试将其设置为cookie,但cookie没有被设置。下面是javascript函数: function positions(){ var widgets = ''; var col = document.getElementById('col'); for(i = 0;

下面是javascript函数,它通过ajax调用将变量“widgets”发送到php文件。如果我随后将变量小部件发送回html文档并将其回显,它将正确地显示在客户端屏幕上。但是当它被发送到php时,我尝试将其设置为cookie,但cookie没有被设置。下面是javascript函数:

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;
}
我删掉了一些函数位置代码,使其更具可读性,因此上面的“小部件”比您预期的要长一些,但这就是它所采用的形式。也许这太多了,不能储存在饼干里


有人知道为什么它没有变成饼干吗?请帮助。

您确定数据已在PHP端发布并接收吗?使用
var\u dump($\u POST)
检查此项

如果是的话,你确定你的会话没有过期吗?由于设置cookie时没有过期时间,因此会话结束时cookie将被擦除

试一试

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


cookie还没有设置吗?

这就是我如何设置和删除cookie,而不会在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); 

?>  

作为说明。。。对于你的cookie来说,这根本不可能有太多的数据。Firefox和Opera过去的最大值约为4090,IE则更高。不确定这是否是有效的信息,但如果cookie大于8190字节,Apache通常会出错,请求根本不会被提供。我在php文件中添加了var_dump($_POST),刷新页面时什么也没有发生,这是否意味着数据没有被php端接收到,未设置cookie。如果您将其添加到接收帖子的脚本中,则是,您的帖子数据未发送到服务器。您可以使用JQuery轻松地发送数据:
$.post(“foobar.php”,{firstname:“Foo”,lastname:“Bar”})确定。因此我必须添加$.post(“ajaxwidgetpositions.php”,{widgetpositions:widgets});删除我的javascript代码中for语句和return true部分之间的所有内容?谢谢,虽然不知道你的数据结构,它可能是好的。但是,您必须将JQuery库包含到您的文件中,对数据进行JSON编码可能是明智的。谷歌搜索“JQuery post ajax json ecode”或类似内容,您将获得所需的信息。
<?php
$widgetpositions = $_POST["widgetpositions"];
setcookie("widgetss", $widgetpositions, time()+60*60);
?>
  //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); 

?>