0

I have a problem with the "windows.location" command in JavaScript. I would like to add the php variable in the link windows.location. How can i do? For example: I would like to transfer user to English page or Vietnamese Page by variable $lang

Here is my code

echo 'window.location="/B2C/$lang/confirm_fone.html"';

and the result in address bar is:

http://10.160.64.4:1234/B2C/$lang/confirm_fone.html

the $lang in address bar cannot be decode?

6 Answers 6

7

Variables in single-quoted strings don't get interpolated in PHP.

Use this instead:

echo 'window.location="/B2C/' . $lang . '/confirm_fone.html"';

Or use doublequotes:

echo "window.location='/B2C/$lang/confirm_fone.html'";
Sign up to request clarification or add additional context in comments.

Comments

2

This is because the whole string is in single quotes.

You'll want to use double quotes for interpolation.

Otherwise, you can try:

echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';

Comments

0

If you put php variables within the string you should use Double Quotes .

echo "window.location='/B2C/$lang/confirm_fone.html'";

Comments

0

You have to concatenate the value, as follows :

echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"';

Comments

0

Variables are not resolved by PHP in Strings when you use the ' as delimiter. Use " instead (and ' for the javascript command) or concatenate the String using ..

Comments

0

If you are in php-context:

echo "window.location=\"/B2C/"{$lang}"/confirm_fone.html\";';

If you are in HTML-Context (or better "outer-php-context"):

window.location="/B2C/<?php echo $lang ?>/confirm_fone.html";

Comments

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.