views:

653

answers:

3

I am using jquery's .post ajax call to pull an form input value and pass it to a php file that starts a session and stores the value in it. The session value is then called on a different page.

The problem is, this all takes place without a page refresh, so the session value is always one page refresh behind. I.E. the first time the session value is called it is blank, but after refresh the value is loaded with the initial input value. When refreshing again, the session's second value is pulled. So, the session value is never current... it is one behind.

How can make it so the php session variable is current without needing to do a refresh?

Or, if this needs to be solved with a refresh, what is the best way to go about it?

Thanks for your help!

A: 

You can access the session id from the cookies assuming you are using the default setting for php sessions to use cookies. Javascript can access cookies.

However, do you really need the session id? The browser will send it for the next AJAX request you send so you shouldn't really need to do anything unless your doing something more complex with multiple session ids....

spatel
This project is a little complex. I am pulling the variable on one page with jquery and passing it to another page that is entirely php and loaded in a popup on the original page. All without page refresh. So, because the variable can only be accessed using php, I thought that sessions would be the best solution.Now knowing this... what do you suggest?
Joe
As long as everything is being done on the same domain then they don't need the session id. Browsers send all cookies for a given domain and/or path with every request. So if you have a popup and it sends another request be it AJAX or a form submit it will include all cookies along with the cookie uses to store the session id.
spatel
The form does not submit. I am stopping it with jQuery, and then passing the value via .post. How would this affect cookies? can you show me an example of how to pass it?
Joe
Spatel, I tried using jQuery Cookie, but had the same results of needing to refresh the page for the value to get passed. Have any ideas how I could avoid needing to page refresh?
Joe
A: 

If you want to manipulate with this value just return it via jSON and do your logic in callback function.

Darmen
Can you please provide a link or example... I am not familiar with json
Joe
You can find and example here - http://docs.jquery.com/Ajax/jQuery.post . At the bottom of the page.
Darmen
A: 

I did not end up needing to use Sessions, or Cookies.

I found a solution to my problem here:

http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

The issue was my input that the variable needed to be passed to was on a different page. Because I loaded this page with an iframe, I could carry the variable to the input with jQuery without the need for php sessions or php cookies. I used the technique that is in the linked post above.

Thanks to everyone who helped out.

Joe