1

I stored a javascript value into a cookie, as shown below.

var headvalue = "blue";

localStorage.setItem('headvalue', headvalue );

It it possible to get this headvalue variable from the cookie and store it into a php variable $headvalue? if yes how?

1
  • 2
    localStorage != cookies. If it was a cookie and it was sent to the server, then yes, PHP could access it (upon the next request). Commented Feb 7, 2014 at 2:16

2 Answers 2

7

Create the cookie with Javascript:

var headvalue = "blue";
document.cookie = "headvalue="+headvalue;

Retrieve it with PHP:

$headvalue = $_COOKIE["headvalue"];
Sign up to request clarification or add additional context in comments.

1 Comment

How to delete this cookie value ? I tried, Mage.Cookies.clear('headvalue'); but doesn't work
1

What are you trying to do? I suppose you could do that with AJAX by calling a PHP page with GET/POST, submitting your variable and storing it, but it isn't going to do you much good after that script has completed executing, unless you stored it as a session variable.

8 Comments

i had actually previously asked a question as to why my ajax wasn't working and the answers showed that my ajax had no errors, as i am quickly running short on time from a deadline im looking for a quick alternative to store the javascript variable into php.
That's fine, but I'm trying to understand your application to lead you in the right direction. Are you trying to get the JS variable upon the next page load, immediately after it's set... how is this working?
the javascript variable changes in accordance to the users choice, via buttons on the same page. Therefore leaving session to be useless as the value constantly changes in javascript. I want to get the javascript variable move it to the next page and store into a php variable. Afterwhich i'll be able to store it into a SQL database.
Okay, is the JS variable the only thing getting passed to the next page or is it part of a form with more information? How is the user getting to the next page? Clicking a next button?
$e is the JS variable, so set that to whatever you want to pass along. You'll get the variable in PHP by doing $_GET['myvar'] So actually, you need to do: "testsite.com/?myvar="+e;
|

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.