我的代码没有返回用Jquery生成的cookie的正确值


My code doesn't return the right value of a cookie generate with Jquery

我试图在wordpress网站上设置两种不同类型的用户,访问者或参展商。为此,我在header.php的顶部设置了两个链接。

<a href="" id="">visitor</a>
<a href="" id="">exhibitor</a>

使用jquery.cookie.js,我在js文件main.js中设置了一个名为user_type的cookie。

$('a#btn-visiteur').click(function(){
    $.cookie('user_type', 'visitor', { expires: 7, path: '/' });
});
$('a#btn-exposant').click(function(){
    $.cookie('user_type', 'exhibitor', { expires: 7, path: '/' });
});

回到我的header.php,我只是想显示我的cookie设置正确,所以我只是回显我的文件中的cookie。

<?php if(isset($_COOKIE['user_type'])){
        if($_COOKIE['user_type'] == 'visitor'){
            echo 'visitor';
        }
        if($_COOKIE['user_type'] == 'exhibitor'){
            echo 'exhibitor';
        }
} ?>

使用Firebug,我看到我的cookie是正确设置的,但我的php代码没有返回正确的结果。它只显示cookie的前一个值。

我希望这是可以理解的,我不太擅长英语。亲爱的stackoverflow的人,我需要你的帮助。

谢谢。

设置好变量后重新加载页面。

的例子:

$('a#btn-visiteur').on('click',function(){
    $.cookie('user_type', 'visitor', { expires: 7, path: '/' });
    location.reload();
});