dqwh1218 2015-08-27 06:08
浏览 37

将变量从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!

  • 写回答

2条回答 默认 最新

  • douyan1882 2015-08-27 06:24
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败