1

Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?

I have to do a function that automatically take care of passing variables from PHP to JavaScript in a simple and safe. Currently stored in a class variable array with the values ​​of the variables, and then the idea is to extract in Javascript, but many doubts assail me what the (technically) best way to do this.

Also passes through my head several problems, especially when complex move to Javascript arrays or strings with accents, special symbols or symbols as quote or single quote, including problems with the function json_decode when work with chains with line breaks and carriage returns. (You get invalid JSON, I've had to do a cleaning function in PHP to avoid passing variables with these characters, but sometimes I need be)

I searched the web a lot, and I have found many answers but none terminade convince and more with the above problems.

An example:

The PHP Method (too simple):

   var JsVars;
   public function saveJsVar($key, $value) {
      $this->JsVars[$key] = $value; //Possible need to clean certain characters like /n /r
   }

The "javascript part"

<script>
   var SistemVars = SistemVars || {};
   <?php foreach ($JSVARS as $k => $var) { ?>
   SistemVars.<?=$k?> = '' || '<?php echo $var ?>'; // Dont work with arrays only plain
   <?php } ?>
</script>

Another option:

<script>
   var SistemVars = SistemVars || {};
   SistemVars = <?=json_encode($JSVARS)?> // Object, with plain and array BUT problems with certain characters.
</script>

The idea its use PHP (saveJsVar) to save plain variables and arrays, and set the vars in SistemVars object en JavaScript to use it like alert(SistemVars.ParamOne);

with your experience, what do you think is the best way to do this?

Community
  • 1
  • 1
Zenth
  • 769
  • 2
  • 7
  • 23
  • i use the same approach when creating javascript data from php. Haven't found any better solution yet. – andrew Sep 24 '12 at 13:05

2 Answers2

1

The 2nd option is the better way to do it, as it saves using a foreach loop. also, json_encode is made for this purpose, so why not use it :) And just FYI, <?= ...?> short tags for PHP are not used anymore Are PHP short tags acceptable to use?

  <script>
  var SistemVars = SistemVars || {};
  SistemVars = <?=json_encode($JSVARS)?> // Object, with plain and array BUT problems with certain characters.
</script>
Community
  • 1
  • 1
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
  • yes json would work fine, but it needs one more http request. So it comes down to data volume to choose which method best suits you and your applcation – andrew Sep 24 '12 at 13:08
  • Ok, thanks for the response. The unique active problem its the "carriage return problem", and special characters. I referer to this: http://stackoverflow.com/questions/395379/problem-when-retrieving-text-in-json-format-containing-line-breaks-with-jquery I think its need to check if the value is an array, an then, run level by level of array cheking the value of the stored data in array and pass a cleaning function. – Zenth Sep 24 '12 at 13:22
  • @Zenth, use JSON. Don't reinvent your own buggy wheel. Bonus: result can be parsed in JS natively as a statement, without even using any function. – Oleg V. Volkov Sep 24 '12 at 13:57
0

I like using hidden input elements and use/access their values with jQuery. Doing this will allow you to keep your js clean - with no PHP code inside. If you want to export all of your JS code into external files - this is the way.