Reputation: 323
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
Upvotes: 0
Views: 9672
Reputation: 40639
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?
Upvotes: 0
Reputation: 5784
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.
Upvotes: 1