3

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!! :)

9
  • are you working on the localhost? Commented Apr 25, 2012 at 19:51
  • Can you show how you set the cookie in PHP? Commented Apr 25, 2012 at 19:52
  • @Dan Lee: yes, I work on localhost, but as mentioned also tried 127.0.0.1 and even uploaded it to a webserver, still the same problems. Furthermore, the other cookies work fine, set the same way... Commented Apr 25, 2012 at 19:55
  • @AtesGoral: I do not set the cookie in PHP, I set the cookie using jQuery and only read the cookie with php in the file I load... Commented Apr 25, 2012 at 19:55
  • Sorry I tend to overread the little Note thingies :) Could you check in the developers console (F12) your Resources -> Cookies tab, if it exists in there? Commented Apr 25, 2012 at 19:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

well, I have no idea why this occurred only with this cookie, but thank you again Kristian, I managed to solve it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.