-3

I have a PHP file that creates a randomized string stored in a variable. I want to access this variable with JavaScript but the methods I've found online doesn't seem to be working. This is the code I have so far:

var test = "<?php echo json_encode($myVariable); ?>";
alert(test);

$myVariable is just a string: "testing".

I thought "testing" would be alerted but instead the code itself is (<?php echo json_encode($myVariable); ?>). When I take away the quotations, nothing happens. I'm very new to JavaScript so I'm not sure what's wrong. Is there another way I can access a PHP variable with JavaScript?

j08691
  • 204,283
  • 31
  • 260
  • 272
Carmen
  • 25
  • 1
  • 1
  • 4

4 Answers4

0

you can transfer variable contents by this

var test = "<?php echo $myVariable; ?>";
alert(test);
mortypk
  • 46
  • 1
  • 11
0

There are few way of accessing variables from php with javascript AJAX/Cookies/_SESSION or echo directly to javascript.

AJAX

Is more readable with better separations between layers it also allows for async transfer. BUT it can be very slow adding HTTPrequest can lead the website to slow down alot if there is alot of variable to request from php.

Cookies

I dont recommend it as there will be alot of necessary data store on the client side.

_SESSION

I recommend you using _SESSION as new developer will find it easier to use. it works similar to cookies but the only difference is once the page is closed the data is erase.

Echo directly to javascript

Easy to implement but horrible code practise.

All the above answers I explain quite vague do your research on topics, look for _SESSION to start with then move on to AJAX if appropriate.

Code example

PHP

if you are getting data from mysql

$_SESSION["AnyVariable"] = my_sql_query ("SELECT someData FROM someTable")

Or if its just a variable in PHP

$SESSION["aVariable"] = whateverValue;

JavaScript

<script type="text/javascript">
    function someFunction()
    {

        var someVariable = '<%= Session["phpVariableSessionName"] %>';
        alert(someVariable);
    }
</script>
miken32
  • 42,008
  • 16
  • 111
  • 154
-1

json_encode produces output ready for javascript, you must not put it into quotes:

var test = <?php echo json_encode($myVariable); ?>;
Marek
  • 7,337
  • 1
  • 22
  • 33
  • 1
    While this is true, it is not the problem here. – MightyPork Jul 01 '14 at 15:20
  • If I don't include the quotations, then nothing happens at all. – Carmen Jul 01 '14 at 15:21
  • @Carmen Nothing happens because the Javascript is unable to understand the PHP tags which are not being processed on the server. – nullability Jul 01 '14 at 15:24
  • *"I have a PHP file that creates a randomized string stored in a variable."* How is this not being processed on the server? It doesn't look like it's not a PHP file. However since OP hasn't come back with a definitive answer, rather than downloading answers that are potentially correct, we can wait or give up. – Popnoodles Jul 01 '14 at 16:04
-1

Try it the other way around ...

<?php echo "var test = '" . json_encode($myVariable) . "';"; ?>    
bsoist
  • 775
  • 3
  • 10