将变量从php传递给js? 还是更好的方法?

Hi Im trying to get a 3rd party livehelp to autofill with joomla users name and email when logged in.

This is the js code to autofill:

LHCChatOptionsPage.attr_prefill = new Array();
LHCChatOptionsPage.attr_prefill.push({'name':'email','value':'test@email.com'});
LHCChatOptionsPage.attr_prefill.push({'name':'username','value':'Username here'});

And this is the php code to get the variables from joomla:

$user =& JFactory::getUser();
$user_name = $user->name; 
$user_email = $user->email;

But how do I pass the php variables to js? Or is there a better approach I should be taking?

Any help would be much appreciated!

You can echo the variables by php while render the page, just like:

<?php $user =& JFactory::getUser(); ?>
<script>
  ..
  LHCChatOptionsPage.attr_prefill.push({'name':'username','value':'<?php echo $user->name; ?>'});
  ..
</script>

or you can get the variables you want through ajax, in this case you need a php api to provide what you want.

The best approach here would be store the key pairs in a session or cookies, and then try accessing them through AJAX call in your js code. This would mean a clean and standard code.