0

I am trying to get the info from a PHP variable in to JavaScript while in the PHP if statement. 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.

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

1
  • Are you sure with this statement $user =& JFactory::getUser();, shouldn't this be $user = &JFactory::getUser(); Commented Feb 6, 2013 at 12:15

6 Answers 6

1

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 .

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.

0
0

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:

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

etc..

and like the others are saying you can't put an echo inside a string.

0

You can't access php variable like that in last echo, to get user->id dynamicly use this

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

javascript code is regarded as string in 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>';
0

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. 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>
-1

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. just $user_id will work

5
  • This won't work since the string's delimited by ' instead of ". PHP won't try to parse variables in single-quote strings. Commented Feb 6, 2013 at 12:31
  • what if the single quote is replaced with double ? Commented Feb 6, 2013 at 12:33
  • Assuming you escape nested quotes properly, that will work for simple variables. I don't know if it works for things like $user->id Commented Feb 6, 2013 at 12:35
  • when i run the code i get values like this <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> Commented Feb 7, 2013 at 11:30
  • and when user i logged in the php variable does not get updated i get values like this <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> Commented Feb 7, 2013 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.