Deaddancer Posted October 3, 2008 Share Posted October 3, 2008 I have been grappling with a way to get a variable from javascript to PHP and finally gave cookies a try. I have a confirm pop up in my js, and based on what they click (OK/Cancel) I either set a cookie (load_all_data=Y) or I delete it. Then back in my PHP I do a comparison operation; (if ($_COOKIE["load_all_data"]=='Y') { blah....blah..... } else { blah....blah..... } and it ALMOST works perfectly. The issue seems to be that when I process this PHP code, the cookie value is always one step behind, as if PHP reads the value of the cookie before it's assigned. Does this make sense? Basically, my confirm message logic works flawlessly the 2nd time you click one of the options, but as soon as you change to the other option, it doesn't work until you do it once more, and vice versa when switching back (i.e. OK vs. Cancel) Is there a solution to this or is it inevitable that PHP is going to read the cookie before I have a chance to either create or delete it? Here is the relevant code; PHP echo "<script language=javascript>ConfirmChoice()</script>"; if ($_COOKIE["load_all_data"] == 'Y') { echo '<script language="javascript">alert("Loading all records, press OK and wait for data to load")</script>'; } else { echo '<script language="javascript">alert("Press OK to load first 20 records")</script>'; break; } Javascript] function ConfirmChoice() { answer = confirm("You have requested a large amount of audit data which can affect screen performance. Press OK to continue loading audit data or Cancel to display the first 20 records."); if (answer) { alert('Cookie should be created here...'); document.cookie = "load_all_dataY"=; alert(document.cookie); } else { alert('Cookie should be deleted here...'); delCookie("load_all_data"); alert(document.cookie); } Please ignore all the extraneous alerts, that's me trying to figure out what's going on and when... Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 The best way to pass information back and forth from JS to PHP is to use AJAX. Look through some of the tutorials in the AJAX section to get started, or check out this tutorial: http://www.w3schools.com/ajax/default.asp Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 3, 2008 Share Posted October 3, 2008 The reason the cookie value is one step behind is because cookies are only sent to the server when a http request is made. So, it would take refreshing the page or browsing to another page on the site to get the cookie method to work. Since AJAX works by making a http request to the page, you might as well save the extra work of using a cookie and just use AJAX directly. Quote Link to comment Share on other sites More sharing options...
Deaddancer Posted October 3, 2008 Author Share Posted October 3, 2008 Hmm, It doesn't seem obvious to me why AJAX would work for this case. I understand that with Ajax, one can communicate with the server (from the client side) without requiring an HTML request, but how would that help me get a simple confirm (true/false) value from JavaScript to my PHP code? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 That's exactly what AJAX is, JavaScript passing data to PHP and getting responses back and doing something with it. So, again, check out the AJAX tutorials and you'll be able to do what you want. Quote Link to comment Share on other sites More sharing options...
Deaddancer Posted October 3, 2008 Author Share Posted October 3, 2008 I will definitely check out AJAX as a solution for this. Thanks for the speedy replies! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.