html页面加载php的setcookie,js / php设置cookie并从select元素重新加载页面(js/php set cookie and reload page from a sele...

js / php设置cookie并从select元素重新加载页面(js/php set cookie and reload page from a select element)

我刚刚测试了这个在一个更大的项目中工作,其中设置的变量将用于其他设置。 选择select元素中的项目时,将设置cookie并重新加载页面,并使用cookie值设置要在重新加载的页面上使用的变量。 在下面的测试代码中,回显变量值,并将列表中的默认选定项设置为匹配变量值。

一切似乎在FireFox中运行良好,但是在Chrome,Safari和IE中进行测试时,它无法做任何事情。 我已经尝试将onMouseDown更改为onClick和不同的重载方式,如location.reload(true);

我错过了什么或做错了吗?

function setCookie(cvalue) {

document.cookie = "pick=" + cvalue + "; expires=Fri, 31 Dec 9999 23:59:59 GMT";

}

function selectreload() {

window.location.assign(document.URL);

}

$servernames = array("Server 1", "Server 2", "Server 3", "Server 4");

if(!isset($_COOKIE['pick'])) {

echo "No Cookie is set!
";

} else {

$serverselected = $_COOKIE['pick'];

echo $serverselected;

}

?>

Choose your server

foreach($servernames as $servername) { ?>

onMouseDown="setCookie('= $servername ?>'); selectreload();">= $servername ?>

} ?>

感谢您的输入。

I've just been testing this out to work in a bigger project where the variable set in this would be used for other settings. When the item in the select element is selected a cookie is set and the page is reloaded with the cookie value setting a variable to be used on the reloaded page. In the test code below the variable value is echoed out and the default selected item in the list gets set to the matching variable value.

Everything seems to work fine in FireFox however when testing in Chrome, Safari and IE it just fails to do anything. I've tried changing onMouseDown to onClick and different reload ways like location.reload(true);

Am I missing something or going the wrong way about it?

function setCookie(cvalue) {

document.cookie = "pick=" + cvalue + "; expires=Fri, 31 Dec 9999 23:59:59 GMT";

}

function selectreload() {

window.location.assign(document.URL);

}

$servernames = array("Server 1", "Server 2", "Server 3", "Server 4");

if(!isset($_COOKIE['pick'])) {

echo "No Cookie is set!
";

} else {

$serverselected = $_COOKIE['pick'];

echo $serverselected;

}

?>

Choose your server

foreach($servernames as $servername) { ?>

onMouseDown="setCookie('= $servername ?>'); selectreload();">= $servername ?>

} ?>

Thanks for your input.

原文:https://stackoverflow.com/questions/35383185

更新时间:2020-10-20 16:10

最满意答案

尝试这个。 我替换了您生成的PHP列表并将其替换为JS,以避免使用PHP。 在这里,当您选择服务器并点击“保存”时,它将创建一个cookie,然后当您重新加载时,它将根据保存的选择自动在下拉列表中选择正确的选项。

function getCookieValue(a, b) {

b = document.cookie.match('(^|;)\\s*'+a+'\\s*=\\s*([^;]+)');

return b ? b.pop() : '';

}

var doc = document.getElementById('list');

var list = ['server1','server2','server3','server4'];

list.forEach(function(val,ind) {

var opt = document.createElement('option');

opt.text = val;

opt.value = val;

if (getCookieValue('pick') === val) {opt.selected = true;}

doc.appendChild(opt);

});

doc.addEventListener('change', function(){

var server = doc.options[doc.selectedIndex].value;

document.cookie = 'pick='+server+'; expires=Fri, 19 Feb 2016 23:59:59 GMT';

location.reload();

});

和HTML一起使用它。

请注意,虽然JS工作正常jsfiddle不保存cookie,所以你必须在自己的应用程序上测试它。

编辑:我修改了上面的代码,因此它将cookie保存在“更改”而不是按下按钮。 它还会同时重新加载页面。

Try this. I replaced your PHP generated list and replaced it with JS instead to avoid using PHP. Here, when you select the server and hit 'Save' it will create a cookie, then when you reload it will automatically select the correct option in the dropdown list based on the saved selection.

function getCookieValue(a, b) {

b = document.cookie.match('(^|;)\\s*'+a+'\\s*=\\s*([^;]+)');

return b ? b.pop() : '';

}

var doc = document.getElementById('list');

var list = ['server1','server2','server3','server4'];

list.forEach(function(val,ind) {

var opt = document.createElement('option');

opt.text = val;

opt.value = val;

if (getCookieValue('pick') === val) {opt.selected = true;}

doc.appendChild(opt);

});

doc.addEventListener('change', function(){

var server = doc.options[doc.selectedIndex].value;

document.cookie = 'pick='+server+'; expires=Fri, 19 Feb 2016 23:59:59 GMT';

location.reload();

});

And the HTML to go with it.

Please note that while the JS is working perfectly jsfiddle does not save cookies so you'll have to test it out on your own application instead.

Edit: I modified my above code so it saves the cookie on 'change' instead of a button press. It will also reload the page at the same time.

2016-02-15

相关问答

保存cookie时,请尝试存储页面信息。 如果您位于页面http://mydomain.dev/purchase_orders/create/2 ,请尝试将cookie保存为 $.cookie('Table_Rows-2', JSON.stringify(data), {expires: 7, path: '/'});

每当您尝试加载cookie时,请使用页码,如果您在第3页,则不会加载Table_Rows-2 cookie。 Try storing the page information w

...

您可以使用会话并通过GET而不是cookie传播会话ID。 请参阅: http : //php.net/manual/en/session.idpassing.php You can use a session and propagate the session id via GET instead of cookie. See: http://php.net/manual/en/session.idpassing.php

正如我在我的评论中提到的,尽管它的描述很模糊而且不太理解,但我认为这个问题可能是因为您正在尝试在发送到服务器之前先读取cookie。 Cookie的工作方式如下所示: 你提出请求 服务器将cookie头返回给客户端 页面加载 - Cookie在此页面加载时对PHP 不可见 刷新 客户端发送cookie头到服务器 服务器接收cookie头,因此PHP可以读取它 页面加载 - Cookie在此处可见。 如果您还没有尝试过,请再次刷新! 既然您想在设置它的同时阅读它,只需存储您设置的值并使用它。 或者(

...

你可以将一个值存储在一个cookie中,但是你不能将一个类(或id)归入一个类(尽管显然你可以命名一个cookie,但它不是完全相同的东西)。 相反,你可以这样做: $.cookie('class','pinned',{expires:30});

然后检索值: var pinnedClass = $.cookie('class');

$('#navigation').addClass(pinnedClass);

You can store a value in a cookie, but y

...

如果我理解你的问题,至少有两种不同的方式: 加载一个初始页面,目的是将(通过JavaScript或Refresh标题)重定向到主页面; 在第一个请求中加载整个主页面,其中包含一个占位符块。 然后设置cookie。 然后使用AJAX技术填充占位符(使用JS发送另一个请求,并用新生成的替换placeholer的HTML内容)。 对于第二种方法,您甚至不需要cookie,因为JS可以使用查询字符串 ( GET请求参数)传递值。 If I understand your question right, t

...

下面是一个解释而不是答案的尝试。 假设您已经在目标页面。 以下小书签适合你吗? 这个: javascript:(function() {

document.getElementById('Dark').click();

})();

或这一个: javascript:(function() {

document.cookie='theme=dark; domain=.example.org; path=/';

window.location.reload(true);

})

...

这是一个使用Cookie的例子的工作演示 $(document).on('mouseenter', '.select-user-type-js', function() {

$('.select-user-type-js.active').add($(this)).toggleClass('active');

if ($(this).hasClass('select-user-type_customer')) {

$('#buyer').show();

$('#seller

...

您可以指定domain ;domain=.example.com以及path ;path=/ (“/”在整个域中设置cookie) document.cookie = cname + "=" + cvalue + "; " + expires +";path=/";

You can specifiy domain ;domain=.example.com as well as path ;path=/ ("/" set cookie in whole domain) document.cooki

...

不要在没有任何条件的情况下调用onObjectDrag() ,或者在将cookie设置为零之后调用onObjectDrag() ,否则最终会导致递归。 function onObjectDrag() {

setCookie('saved', 1, 100);

location.reload();

}

$( document ).ready(function() {

var x = getCookie('saved');

if (x == 1) {

...

尝试这个。 我替换了您生成的PHP列表并将其替换为JS,以避免使用PHP。 在这里,当您选择服务器并点击“保存”时,它将创建一个cookie,然后当您重新加载时,它将根据保存的选择自动在下拉列表中选择正确的选项。 function getCookieValue(a, b) {

b = document.cookie.match('(^|;)\\s*'+a+'\\s*=\\s*([^;]+)');

return b ? b.pop() : '';

}

var doc = document.g

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值