This is probably a newbie question, but i really need help.
I am creating a wishlist in an online store, and if the user is logged in, the wishlist item is going to be saved in the database and non logged in user’s wishlist will be saved via cookies and I am using AJAX for the wishlist functionality. Everything goes well except that the cookies are not updated in the first ajax request.
This is my first steps(ajax):
- call ajax request to update wishlist
- reload wishlist template based on the new cookie
the above steps doesnt update automatically the wishlist template $_COOKIE[] needs a page refresh.
This is my updated steps(ajax)
- call ajax request to update wishlist
- call another ajax request to reload wishlist template
the above code solves the problem, (the number 2 step is called to just refresh the $_cookie value.), but it takes two ajax requests to update the wishlist, which takes a little time to work. Im sure, this is not the best solution. I know this is a better solution for this.
Help pls 🙂
Thanks
Cookies can be set by javascript alone. As you are using ajax I think you can handle cookies fine without any page refresh.
Here is the logic you should implement.
0, PHP can populate$_COOKIEsuperglobal. It’ll set the cookies in browser when the request is finished.0just save the data in cookie. This cookie is available viadocument.cookie. This can be easily done by jquerysample code using jQuery
Here I have used jquery cookie plugin