Wern Ancheta
Wern Ancheta

Reputation: 23297

How to set php cookie via ajax

I've done a little browsing on this site regarding this topic. And found out that its possible to set php cookies via ajax. But I don't know why I can't set cookies on an ajax called php script. I even set a default value, just to make sure that the cookie is getting something:

setcookie("tradename", 'mono', time()+3600);    
setcookie("bus_id", 'loque', time()+3600);  

The ajax called php script has mysql queries on it. And they're inserting records into the database. The setting of cookies happens before the queries.

But I don't know why I'm not getting anything when I use print_r from another page:

print_r($_COOKIE);

I'm using jquery's ajax() function. What do I do to detect what's wrong, please help. Thanks.

Upvotes: 1

Views: 1242

Answers (1)

scaryzet
scaryzet

Reputation: 923

You don't need ajax to set cookies from js. Look at "jquery cookie" plugin here, maybe you will like it. Example usage is at the top of jquery.cookie.js.

Upvotes: 2

Related Questions