Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I need to change the value of a PHP variable to JavaScript code.

The value of the PHP variable has to be something out of (JavaScript) localStorage.

So it is something like this.

$variable = localStorage.anameofalocalstorage; //The localstorage part is js

How can I do this?

share|improve this question

marked as duplicate by Shog9 Jul 5 '13 at 16:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Only JavaScript can read localStorage. So, if you want PHP to get the value, you need to send it from JavaScript; possibly via AJAX. –  Rocket Hazmat Jul 5 '13 at 16:29
    
You'll need to get your variable to the erver through a get or post request, eventually in Ajax... no other way –  Bartdude Jul 5 '13 at 16:29
    
    
@RocketHazmat Oh.. thanks, but i don't really know Ajax.. can you please give me the code for doing that ? –  Evert De Spiegeleer Jul 5 '13 at 16:32
1  
@EvertDeSpiegeleer: There are lots of AJAX tutorials out there, have a look around :-) –  Rocket Hazmat Jul 5 '13 at 16:33

1 Answer 1

This is an example code using jquery for the ajax call.

<?php

$variable_that_should_be_set=isset($_GET['shoudBeSet'])?$_GET['shoudBeSet']:'notSet';

?>

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    </head>
    <body>
        <div id="div1"><?php echo $variable_that_should_be_set;?></div>
        <a href="#" id="button">Click here to Set</a>
        <script>
            $("#button").click( function () {
                $.get("?shouldBeSet=this_is_set")   
            })
        </script>
    </body>
</html>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.