Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: $.cookie different value than PHP $_COOKIE? (only webkit-browser)

I use jQuery to update and load search results and the page navigation at the bottom of the page. I have a jQuery slider and several HTML-Forms with checkboxes, for each of those elements I set a cookie (on change), and use .load() to load the content from load_search.php, which works very well in all browsers.

Now I use the same method to upload the page navigation, and I get very weird results. In the search.php, the jQuery code is:

// PAGE NAVIGATION //

$('#page_navigation').on("click", ".page_no_item" , function(event) {
      var id = (event.target.id);
      $.cookie("cur_page_no", id);
      search_results_load();
      page_navigation_load();
      alert($.cookie("cur_page_no")); // -> returns the right cookie value
    });

also, I check the value on document ready, to be sure:

$(document).ready(function() {  
      alert($.cookie("cur_page_no")); // -> also returns the right cookie value
    });

In the .php file that I load, in no-webkit browsers, $_COOKIE['cur_page_no'] returns the right value, just in webkit browsers, it always returns 1 (which I put as Standard, if the Cookie is empty or not set). var_dump() returns always string(1) "1" in webkit browsers.

Note: I have tried to run it not on localhost, but the problem remains. Please also note, that I use the exactly same way to read the other cookies (e.g. for the checkboxes which are checked) and they return the right value.

Any ideas or hints are highly appreciated! Thank you very much in advance!! :)

like image 690
Chris Avatar asked May 18 '26 06:05

Chris


1 Answers

When i was facing this problem, it appeared that in several instances, there was actually two cookies by the same name. One of them was the php created cookie, the other was the javascript created cookie. In other instances, there was one cookie, but php would not properly output the updated value until a full page reload had occurred.

It was problematic that the loading of a fresh page which caused the cookie's desired "new" value to be read. So one way to deal with php's late cookie update effect is to both use the cookie methods available to it and also just setting $_COOKIE['yourCookieName'] = $val as well.

The solution I had to stick with, unfortunately, is to handle a particular cookie either completely with JS or PHP and stick with it, and that was the only way i could get it to never have and read / value update / duplication problems

like image 86
Kristian Avatar answered May 19 '26 18:05

Kristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!