-3

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?

6
  • 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. Commented Jul 5, 2013 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 Commented Jul 5, 2013 at 16:29
  • Possible duplicate: stackoverflow.com/questions/4298091/… stackoverflow.com/questions/1917576/… stackoverflow.com/questions/9836748/… Commented Jul 5, 2013 at 16:31
  • @RocketHazmat Oh.. thanks, but i don't really know Ajax.. can you please give me the code for doing that ? Commented Jul 5, 2013 at 16:32
  • 1
    @EvertDeSpiegeleer: There are lots of AJAX tutorials out there, have a look around :-) Commented Jul 5, 2013 at 16:33

1 Answer 1

0

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>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.