在Javascript中添加php代码和php代码中的javascript

时间:2013-02-06 12:12:02

标签: php javascript

我试图在PHP if语句中将PHP变量的信息传递给JavaScript。我不确定是什么问题。我试图逃避PHP代码然后我认为我不能在PHP代码中使用PHP代码标记。

这是我的代码,但这不起作用:

$user =& JFactory::getUser();
if ($user->guest) 
    echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',1,\'User_Status\',\'Guest\',1])</script>';
    echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\'GuestUser\',1])</script>';
} else {
    echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',1,\'User_Status\',\'LoggedIn\',1])</script>';
    echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\' "echo $user->id "\', 1])</script>';
}

你能帮忙吗? 非常感谢 洁

6 个答案:

答案 0 :(得分:1)

应该是

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\''.$user->id.'\', 1])</script>';

我猜...回声中没有回声,并通过.

连接字符串

您也可以尝试以下方法:

<?php 
   $user =& JFactory::getUser();
   if ($user->guest) {
   ?>
      <script type="text/javascript">_gaq.push(['_setCustomVar',1,'User_Status','Guest',1])</script>
      <script type="text/javascript">_gaq.push(['_setCustomVar',2,'UserID','GuestUser',1])</script>
   <?php
   } else {
   ?>
      <script type="text/javascript">_gaq.push(['_setCustomVar',1,'User_Status','LoggedIn',1])</script>
      <script type="text/javascript">_gaq.push(['_setCustomVar',2,'UserID','<?=$user->id?>', 1])</script>   
   <?php
   }
   ?>

这可能更容易阅读。

答案 1 :(得分:0)

引号正在破坏你的字符串!在这种情况下,我通常采用引号之间的字符串部分并将其放在变量中。例如$ setcustomvar =“'_ setCustomVar \'”然后你将它添加到你的echo代码中:

'<script type="text/javascript">_gaq.push([\'.$setcustomvar.'

等。

并且像其他人一样说你不能在字符串中放置回声。

答案 2 :(得分:0)

你不能像上次回声那样访问php变量,以获得用户 - &gt; id动态地使用这个

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\' '.$user->id.' "\', 1])</script>';

答案 3 :(得分:0)

javascript代码在php中被视为字符串。只需将变量值与字符串连接起来即可。

试试这个:

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\'' . $user->id . '\', 1])</script>';

答案 4 :(得分:0)

而不是:

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\' "echo $user->id "\', 1])</script>';

试试这个:

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\' ' . $user->id . '\', 1])</script>';

无法嵌套echo。你必须使用字符串连接。

然而,为了提高效率,我建议:

<?php
    $user =& JFactory::getUser();
    $userStatus = 'LoggedIn';  // Set default values first.
    $userId = $user->id;
    if ($user->guest) {
        $userStatus = 'Guest'; // Override defaults if the user is a guest.
        $userId = 'GuestUser';
    }
?>
<script type="text/javascript">_gaq.push(['_setCustomVar',1,'User_Status','<?=$userStatus  ?>',1])</script>
<script type="text/javascript">_gaq.push(['_setCustomVar',2,'UserID','<?=$userId ?>',1])</script>

答案 5 :(得分:-1)

试试这个会起作用:

$user =& JFactory::getUser();
if ($user->guest) 
    echo '<script type="text/javascript">_gaq.push([\""_setCustomVar"\",1,\""User_Status"\",\""Guest"\",1])</script>';
    echo '<script type="text/javascript">_gaq.push([\""_setCustomVar"\",2,\""UserID"\",\""GuestUser"\",1])</script>';
} else {
    echo '<script type="text/javascript">_gaq.push([\""_setCustomVar"\",1,\""User_Status"\",\""LoggedIn"\",1])</script>';
    echo '<script type="text/javascript">_gaq.push([\""_setCustomVar"\",2,\""UserID"\",\"" $user->id "\", 1])</script>';
}

您不需要在php echo中提供 echo $ user_id 。只需 $ user_id 即可