28

In my application I rely heavily on JavaScript to enhance the user interface, but all of the data comes from a database and is processed by PHP. By default I use 'echo' statements to substitute the required values "just in time" like so:

var myVariable = <?php echo $myVariableInPHP ?>

This, however, does not strike me as very elegant and I am concerned about stability and maintainability of such code.

Do I have any alternatives here?

For server-side, I am using the Symfony 1.4 PHP framework.

Thanks,

Dorad
  • 3,413
  • 2
  • 44
  • 71
Goro
  • 9,919
  • 22
  • 74
  • 108
  • 3
    May I comment on how I find funny that an over 3 years old question is a duplicate of a one month-old question? It's just funny. – Yanick Rochon Jun 02 '14 at 16:39
  • 1
    This might really blow your mind, but the answer to your comment was already posted in that new thread... before you asked it! But seriously, the newer question has better answers and far more views. This was the right one to mark. – JPhi1618 Jun 25 '14 at 02:26
  • 1
    I find it funny that the person who wrote the duplicate question 4 years later marked this one as the duplicate – Nikita 웃 Jan 02 '16 at 17:20
  • @CreativeMind heh. To be fair, the other question is like an essay on the topic, so it's probably a better resource. I was into it before it was cool, I guess. – Goro Jan 28 '16 at 03:33

5 Answers5

39

My favorite way is :

<?php

$var = array(
  'prop1' => 'value1',
  'prop2' => 'value2',
  // ...
);

?>
<script type="text/javascript">
   var varNameSpace = <?php echo json_encode($var); ?>;

   alert( varNameSpace.prop1 ); // -> 'value1'
</script>

Using json_encode() ensures that the values passed to Javascript are escaped and well formatted. Using a common variable container also prevents from over using the global space (window).

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
4

You might want to use JSON for this, it's really simple to use in both PHP (check json_encode()) and JavaScript.

It's safe to use within <script>-Tags and browsers which understand JavaScript. Note that the PHP function doesn't encode < and >.

Some example PHP:

$user = (object) array("name"=>"Joseph", "age"=>29, "email"=>"asdf@example.net");
echo '<script type="text/javascript"> var user = '.json_encode($user).'; </script>';
svens
  • 11,438
  • 6
  • 36
  • 55
2

I'd try to use JSON. Here is a link for you to php.net explaining how to do this.

http://php.net/manual/en/book.json.php

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
1

First of your solution does work, but It is not a good practice to mix client-side code with server-side code. It is a best practice to put javascript in seperate .js files(no PHP in it)

I would first create an API(write documentation) like for example

GET
http://localhost/getProfile?username=$username

POST
http://localhost/getProfile/$username

It will return JSON-object using json_encode. You could use json-p for cross domain communication. Then from for example Jquery you can easily get the data.

This way your javascript would stay clean.

Alfred
  • 60,935
  • 33
  • 147
  • 186
  • I do this with ExtJS applications. The widgets rely heavily on data from the database and it's all in JSON. However, you now have to fight CSRF if your site is supposed to be secure. – AutoSponge Sep 27 '10 at 13:19
0

I prefer use_dynamic_javascript() helper. "Bad" thing about it is you have to think a bit more on splitting rendering template itself and config for it into separate requests.

Andrei Dziahel
  • 969
  • 5
  • 14