views:

493

answers:

5

How to access PHP varibles in Javascript or Jquery? Do I have to write

<?php echo $variable1 ?>
<?php echo $variable2 ?>
<?php echo $variable3 ?>
...
<?php echo $variablen ?>

I know I can store some variables in cookies,and access these values via cookies, but values in cookies are relatively stable values, moreover, there is a limit, you can not store many values in cookies, and the method is not that convenient. Is there other better way to do it?

A: 

Basically, yes. You write alert('<?php echo($phpvariable); ?>');

There are sure other ways to interoperate, but none of which i can think of being as simple (or better) as the above.

The MYYN
There are two questions in the question, which are you answering?
Dominic Rodger
Two questions? Only one question.
Steven
+1  A: 

I would say echo() ing them directly into the Javascript source code is the most reliable and downward compatible way. Stay with that unless you have a good reason not to.

Pekka
A: 

You're asking kind of a two-part question. As far as syntax (I think since PHP4?) you can use:

<?=$var?>

... if PHP is configured to allow it. And it is on most servers.

As far as storing user data, you also have the option of storing it in the session:

$_SESSION['bla'] = "so-and-so";

for persistence from page to page. You could also of course use a database. You can even have PHP store the session variables in the db. It just depends on what you need.

Greg
As you say, this syntax is configured on *most* servers, but not all. For truly portable code, I'd still recommend using the full <?php echo $variable /? syntax. It's only 7 characters more per instance. However, if you have full control over your hosting servers, and you know you are not going to make the code available for other users on different servers, then go for it.
ZombieSheep
you don't have access to $_SESSION inside javascript
Karsten
@Karsten: I wasn't suggesting that you do. I was just suggesting using session variables instead of cookies, when cookies aren't necessary. You would still have to echo them inside your script, of course.
Greg
+12  A: 

Your example shows the most simple way of passing php variables to javascript. You can also use json_encode for more complex things like arrays:

<?php
$simple = 'simple string';
$complex = array('more', 'complex', 'object', array('foo', 'bar'));
?>
<script type="text/javascript">
var simple = '<?php echo $simple; ?>';
var complex = <?php echo json_encode($complex); ?>;
</script>

Other than that, if you really want to "interact" between php and javascript you should use ajax.

Using cookies for this is a very unsafe and unreliable way, as they are stored clientside and therefore open for any manipulation or won't even get accepted/saved. Don't use them for this type of interaction. jQuery.ajax is a good start imho.

Karsten
+1 for mentioning JSON with example.
Pekka
You have quotes round `<?php echo $simple; ?>` and I think that's right. But you have quotes round `<?php echo json_encode($complex); ?>` as well, and I think that's wrong --- `json_encode()` returns valid javascript, so you don't want to quote it, surely?
Dave Hinton
thanks dave, good point :)
Karsten
Interaction between Javascript and PHP on the same PHP file, not on different PHP files.
Steven
There is no PHP/JavaScript interaction in just one file. For one file, there is only one-way PHP -> JavaScript.
Karsten
PHP->JavaScript is ok.
Steven
Anything unclear then? My answer contains just that :)
Karsten
Your answer is good.
Steven
+2  A: 

If AJAX isn't an option you can use nested data structures to simplify.

<?php
$var = array(
  'qwe' => 'asd',
  'asd' => array(
    1 => 2,
    3 => 4,
    )
  'zxc' => 0,
  ); ?>
var data = <?php echo json_encode($var); ?>;
stroncium