Passing variables from PHP to JS and vice-versa

Hi there,

At the beginning I should point out that I did some forum search and found no answer for this question. Maybe this is because, it is late afternoon here, end of hard week and my brain gots a little wako, but I think there is no answer! :)

Here it is. What is best practice for exchanging variables’ value between PHP and JS and vice versa? Right now I’m using hidden inputs fields which can be easily filled by both PHP and JavaScript. But I deeply feel that this is very bad practise. Can anyone advice better one?

Cheers!

Hi Trej, how do you communicate? AJAX? Page Load?

In your head section:


var config = <?php echo CJavaScript($this->config)?>

Then just:




if(config.username)

  alert(config.username);



No. What I mean is that your PHP generates static HTML (sent back to browser) with some variables and this content contains some JavaScript code that can access values of those variables.

But, as samdark explained, it is just to use CJavaScript class. As brillant as easy! :} Is there anything for which Yii does not have a proper class ready? :] :>

Trejder

Yep, there are still some things to implement ;)

Sorry Trej… didn’t understood.

No probem, Antonio! Always pleaser to talk to you! :]

Bear in mind that PHP 5.2 and up come with built in json support that is a little simpler and quicker to use than the Yii supplied helper, just call:


var config = <?php echo json_encode($this->config) ?>

Yii does use the json_encode() & json_decode() functions when they are available, so if you are running php 5.2 or up you can save a bit of code using the php functions directly.

http://php.net/manual/en/function.json-encode.php

Thanks for your advices. I prefer using object-oriented approach over procedural one, even if former is only nice looking package to latter one. Just like in example, you provided where CJavaScript helper is only a package to json_encode().

And I’m using this approach strictly from psychological manners. I just hate all the mess there is in PHP (never so more messy language, though I love it anyway) with all this procedural stuff. This sometimes drives me crazy like for example with strings-related functions where some are named like str_ (str_replace), other str (strtoupper) and even more without any perfix (ucwords), even though they are members of string manipulation family. Sometimes I’m thinking that some people out there are having some mind problems that they are making things so incredibly complicated, where they can be so simple (String object with all methods like toUpper, uppercaseWords and replace).

And what pisses me the most is that even PHP 5 has like nearly nine years (!!!) and for all this long time people were keeping yelling that objectivity is the only future and that procedural coding is dead and even so, even new releases of PHP are still brining that procedural mess that packages new object being added (like all this ugly procedural date manipulation stuff to wrap beautiful DateTime class).

But we are again way off-topic! :]

OK, each to his own :)