简体   繁体   English

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

[英]Add javascript in php code and php code in Javascript

I am trying to get the info from a PHP variable in to JavaScript while in the PHP if statement. 我试图在PHP if语句中将信息从PHP变量获取到JavaScript中。 I am not sure what is wrong. 我不确定是怎么了。 I tried to escape the PHP code then I thought I cannot have PHP code tags with in PHP code. 我试图转义PHP代码,然后以为PHP代码中不能包含PHP代码标签。

Here is my code but this does not work: 这是我的代码,但这不起作用:

$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>';
}

Can you please help? 你能帮忙吗?
Many Thanks 非常感谢
Jai i

Should be 应该

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

I guess... no echo inside of echo and connect string via . 我猜... echo内没有echo并通过连接字符串.

You also might to try out the following: 您也可以尝试以下方法:

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

This might be easier to read. 这可能更容易阅读。

The quotes are breaking your string! 引号破了你的弦! In this cases I usually take the part of string that is between this quotes and put it in a variable. 在这种情况下,我通常将引号之间的字符串部分放入变量中。 For example $setcustomvar="'_setCustomVar\\'" and then you add it in your echo code like this: 例如$ setcustomvar =“'_ setCustomVar \\'”“,然后将其添加到回显代码中,如下所示:

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

etc.. 等等..

and like the others are saying you can't put an echo inside a string. 就像其他人说的那样,您不能在字符串中添加回显。

您无法在上次回声中访问php变量,以动态获取用户-> id

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

javascript code is regarded as string in php. javascript代码在php中被视为字符串。 Just concatenate your variable value with the string and it will work. 只需将变量值与字符串连接起来,它将起作用。

Try this: 尝试这个:

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

Instead of: 代替:

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

Try this: 尝试这个:

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

Can can't nest echo 's. 不能嵌套echo You'll have to use string concatenation, instead. 您必须改为使用字符串连接。

However, for full efficiency, I'd suggest this: 但是,为了获得充分的效率,我建议这样做:

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

Try this will work: 试试这个将工作:

$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>';
}

You don't need to give echo $user_id inside the php echo. 您无需在php echo中提供echo $ user_id just $user_id will work $ user_id将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM