dsegw3424
2013-06-20 11:59
浏览 65
已采纳

在jquery中设置cookie变量

I am trying to set a cookie in jquery using :

 $( "a.Edit" ) .click(function() { 
$( "#dialog-form" ).dialog( "open" );
 var un=$(this ).text();
 $.cookie("test", un);
  });

but when i use it after that <?php echo $_COOKIE['test'] ?> it wont work the cookie is still not set

any help please

thanks in advance

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongtang1910 2013-06-20 12:06
    已采纳

    Use the jquery_cookie() plugin for this.

    $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
    

    Where the_cookie is the name of your cookie. and where the_value of your cookie is the value/function it has to do. expires 7 means that the cookie wil expire in 7 days (one week) Path isn't nessesary,

    Define the path where the cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: '/'. Default: path of page where the cookie was created.

    You can remove the cookie using:

    $.removeCookie('the_cookie');
    

    you can read the cookie using:

    $.cookie('the_cookie');
    

    Hope it helps.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doutuanxiao4619 2013-06-20 12:01

    Try using:

    $.cookie("example", "foo", { expires: 7 });
    

    Source

    评论
    解决 无用
    打赏 举报
  • doomli3721 2013-06-20 12:07

    It is possible to set the cookie in PHP entirely without jQuery at all..

    ..however...

    It appears you are using jQuery in this way.

    What may be causing a problem is a few things:

    a) $(this).val() possibly might be returning NULL.

    b) You are not setting the path and expiration on the cookie. If you have subdirectories it is normally good to set a master cookie that is the root path '/'.

    To read your cookie using PHP, try this...

     $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    

    Can be possible duplicate of Get the cookie value in PHP?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题