0

I have some code like this

  var minAmountPay =  <?php echo $this->chat['Current']['admin'] >= 1 ? 1 : $this->minAmountPay; ?>;
  var userId = <?php echo $this->currentUserId?>;

I use ZF and I have a lot of javascript code in my action files. I want to create js-files for each action and do minifications of this files. But I can't create js-file because I have assigning php variable to javascript variable.

Maybe I need to use some javascript framework for this purposes?

Dmitro
  • 1,489
  • 4
  • 22
  • 40

5 Answers5

3

Use data attributes.

For example :

HTML :

<div id="something" data-minamountpay="<?php echo $this->chat['Current']['admin'] >= 1 ? 1 : $this->minAmountPay; ?>"></div>

JS (with jQuery) :

var minAmountPay = $('#something').data('minamountpay');
Laurent W.
  • 3,629
  • 2
  • 20
  • 29
  • It's less work to directly output the PHP value to the JavaScript variable... – Ryan Jan 09 '14 at 16:07
  • But you can't cache JS files and you don't separate interactivity from pure HTML. – Laurent W. Jan 09 '14 at 16:07
  • This is the proper way to do it. @Stanyer it's less work to not sanitize your database inputs. – AmazingDreams Jan 09 '14 at 16:08
  • @AmazingDreams true, true - bad outlook on things my side... – Ryan Jan 09 '14 at 16:09
  • I think I create view-helper which will be generate js-file with variables. I will have something like this $this->jsVars()->add('name', $value);. And after this I can cache files for each values and i don't like that each time jquery will retrieve values from html. – Dmitro Jan 09 '14 at 16:18
1

JavaScript cannot directly interact with PHP.

This is because JavaScript is client-side, and PHP is server-side.

The only way to do this is as you have - by producing output in PHP which is put into the script.

Ryan
  • 3,552
  • 1
  • 22
  • 39
1

you can add javascript to your html file by using

<script type="text/javascript">
var minAmountPay =  <?php echo $this->chat['Current']['admin'] >= 1 ? 1 : $this->minAmountPay; ?>;
var userId = <?php echo $this->currentUserId?>;
</script>
parchambault
  • 142
  • 4
0

You can assign these values to hidden inputs in your page.

And get the values by input value.

These values are visible to the user either way.

cretzzzu3000
  • 221
  • 1
  • 7
0

You might want to think about where you're putting those variables. Can you put them in the HTML instead? When I need to do what you are doing I use the HTML 5 data attributes and assign the variables to appropriate places like this:

<div id="my-appropriate-div"
    data-min-amount-pay="<?php echo $this->chat['Current']['admin'] >= 1 ? 1 : $this->minAmountPay; ?>"
    data-user-id="<?php echo $this->currentUserId ?>">

Then in JavaScript you can access the data thusly:

var minAmountPay = $('#my-appropriate-div').data('min-amount-pay');
var userId = $('#my-appropriate-div').data('user-id');

Obviously rather than putting them into variables, I just use them directly where needed in my functions.

Styphon
  • 10,304
  • 9
  • 52
  • 86