Jump to content

[SOLVED] Passing data from javascript to PHP using cookies - ALMOST THERE...Help!


Deaddancer

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.