0

我试图在 PHPif语句中将信息从 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>';
}

你能帮忙吗?
非常感谢

4

6 回答 6

1

应该

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

我猜... echo 内部没有 echo 并通过连接字符串.

您还可以尝试以下方法:

<?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
   }
   ?>

这可能更容易阅读。

于 2013-02-06T12:16:53.890 回答
0

您不能像上次回显中那样访问 php 变量,以动态获取 user->id 使用它

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\' '.$user->id.' "\', 1])</script>';
于 2013-02-06T12:17:14.593 回答
0

javascript代码在php中被视为字符串。只需将您的变量值与字符串连接起来,它就会起作用。

尝试这个:

echo '<script type="text/javascript">_gaq.push([\'_setCustomVar\',2,\'UserID\',\'' . $user->id . '\', 1])</script>';
于 2013-02-06T12:17:50.167 回答
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>
于 2013-02-06T12:18:11.850 回答
0

引号正在破坏您的字符串!在这种情况下,我通常会使用引号之间的字符串部分并将其放入变量中。例如 $setcustomvar="'_setCustomVar\'" 然后你将它添加到你的回显代码中,如下所示:

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

ETC..

就像其他人说的那样,您不能在字符串中放置回声。

于 2013-02-06T12:16:14.850 回答
-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即可

于 2013-02-06T12:27:29.460 回答