27

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,

4
  • 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. Commented Jun 2, 2014 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. Commented Jun 25, 2014 at 2:26
  • 1
    I find it funny that the person who wrote the duplicate question 4 years later marked this one as the duplicate Commented Jan 2, 2016 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. Commented Jan 28, 2016 at 3:33

5 Answers 5

38

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).

Sign up to request clarification or add additional context in comments.

Comments

3

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"=>"[email protected]");
echo '<script type="text/javascript"> var user = '.json_encode($user).'; </script>';

Comments

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

1 Comment

Care to explain why I got downvoted?
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.

1 Comment

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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.