I have the code below in a helper function, where I try to redirect the user to a link after an event occurs.

static public function redirect($path)
{       
    echo '<script type="text/javascript">';
    echo 'self.parent.location.href = "$path";';
    echo '</script>';        
}

In my case I call the function as Helper_Popup::redirect("/account/my_addresses") but, of course, it redirects me to a link something/$path. How should I 'embed' the php variable in the above code?

share|improve this question

Maybe as echo "self.parent.location.href = '/$path';"; (note leading slash and quote inversal)? Is that what you're after? – DaveRandom Apr 18 at 12:04
1  
It is impossible to have a php variable in javascript code. That's 2 different languages, having no access to each other variables. – Your Common Sense Apr 18 at 13:21
feedback

6 Answers

The line is incorrect. If you want to use php in Javascript you have to do it so

echo 'self.parent.location.href = "' . $path . '";';

or you can use the double quotes like this

echo "self.parent.location.href = '$path';";

If you use single quotes php will not be executed. But with double quotes php will parse the php code if available and single quotes will write the echo as plain text: "$path"

share|improve this answer
I personally like the first example more then the second. There is a big chance that your editor will not highlight the $path variable in the second example. – WouterH Apr 18 at 12:14
OMG "php will be executed" – Your Common Sense Apr 18 at 13:20
@Your Common Sense: that was wrong described you're right. i've corrected it – alphanyx Apr 18 at 13:35
it remains the same – Your Common Sense Apr 18 at 13:35
feedback

Try.

echo 'self.parent.location.href = "'. $path .'";';
share|improve this answer
feedback

add a <?php ?> tag and echo the variable

Example

<script src='<?php echo $var; ?>' </script>
share|improve this answer
if the "shoprt_open_tag" option is enabled in the php.ini you can also use "<?=$var?>" for your output – alphanyx Jul 4 at 16:03
feedback
echo 'self.parent.location.href = "'. $path .'";';
share|improve this answer
1  
lots of answers - pretty much all the same - very quickly, I'm never fast enough for SO – TerryProbert Apr 18 at 12:06
1  
very rare that I am both correct and fast enough at the same time! – martincarlin87 Apr 18 at 12:07
feedback

You have your $path variable encased in single quotes so the variable is being interpreted literally as '$path' and not the actual value of the variable, which is the difference in php between double and single quotes, try this:

static public function redirect($path)
{       
    echo '<script type="text/javascript">';
    echo 'self.parent.location.href = "'.$path.'";';
    echo '</script>';        
}
share|improve this answer
feedback
echo "self.parent.location.href = '$path';";
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.