为什么这个javascript变量没有设置到php中的cookie中
Why does this javascript variable not get set into a cookie in php?
我有下面的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:
<?php
$widgetpositions = $_POST["widgetpositions"];
setcookie("widgetss", $widgetpositions);
?>
值得一提的是,javascript变量"widgets"的形式为:
&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
我删掉了一些函数位置代码,使其可读性更强,所以上面的"小部件"比你预期的要长一点,但这就是它所采用的形式。也许这太多了,不能放在饼干里?
有人知道为什么它没有变成饼干吗?请帮忙。
您确定数据在PHP端发布并接收吗?用var_dump($_POST)
对此进行检查。
如果是,你确定你的会话没有过期吗?由于您在设置cookie时没有过期时间,因此当会话终止时,cookie将被擦除。
尝试
<?php
$widgetpositions = $_POST["widgetpositions"];
setcookie("widgetss", $widgetpositions, time()+60*60);
?>
饼干还没有凝固吗?
这就是我如何设置和删除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);
?>
相关文章:
- PHP中的setcookie仅适用于localhost
- 如何在php中的jquery中对循环内的选择框值求和
- 通过传递图像的URL通过javascript读取/处理图像-类似于PHP中的file_get_contents
- PHP中的数组输出
- javascript和php中的pancard结构验证
- 带有输入参数的Javascript/PHP中的XMLHTTPrequests
- PHP中的JS.警报没有'不要出现
- php中的脚本重定向是递归的
- 如何计算与php中的CRC32函数兼容的arrayBuffer(javascript中的From FileReader)
- 在php中的服务器上执行之前对输入进行消毒
- php中的javascript var太长
- javascript中的脚本等于php中的$_SERVER['REQUEST_URI']
- 在PHP中的所有页面上都包含页眉
- JQuery Ajax PHP中的错误和成功处理
- PHP/AAJAX-使用PHP中的$_FILES访问javascript文件数组
- 如果函数PHP中的条件为true,则禁用链接
- 想要在php中的一个文件中写入2个JavaScript变量
- 填充文本框取决于php中的下拉选择
- 可以't使用ssl读取php中的cookie
- 在php中的同一网页上检索所选下拉项的值,并使用其他数据将其发布到其他表单