I want to use a localStorage variable in a PHP page. Very simple but "strange".

AJAX comes to mind. Whats the best form to do this?

Thanks

Recommended Answers

All 6 Replies

that's it!!

you can also use session as session is easy to maintain. http://php.net/manual/en/function.session-start.php

Ive had problems with sessions in the past....plus I set this client side: I click on a checkbox, if the checkbox is true, something (cookie, localstorage, session, etc) must be stored that can be accessed EVERYWHERE on the site. Thats the problem I have with a cookie. Sessions get lost for some reason (problably related to the cookie problem) and thats why Im trying with localstorage....

To get the local storage variable to PHP you'll have to use an AJAX call. Whether you use a GET or POST is up to you. Personally, I'd choose a POST.

To get the local storage variable to PHP you'll have to use an AJAX call. Whether you use a GET or POST is up to you. Personally, I'd choose a POST.

What would be the best way to do this then?

example.php

<?php

echo "hi"
$var=getlocalstoragehere.php;
echo $var
?>

Lets say that the page I use. What would be the AJAX page?

Something like this if you use jQuery:

$.ajax({  
    type: "POST",  
    url: "example.php",  
    data: { storageValue: localStorage.getItem("yourValue"); }
});

In PHP you'd use $_POST['storageValue']

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.