2

what I need to do is open a new window to a link that has php variables in it. As far as I know, I cannot use php variables in javascript functions, so I tried this which does not seem to be working

$link="<script type='text/javascript'>window.open('http://www.fonefinder.net/findome.php?npa=$num1&nxx=$num2&thoublock=$num3&usaquerytype=Search+by+Number&cityname=
')</script>";
echo $link;

$num1, $num2, and $num3 are all php variables. Is there a better way to do this? Thanks.

4
  • It doesn't work because of line break... Commented Sep 1, 2012 at 19:54
  • What is the generated HTML/JavaScript (output) of your PHP script for this small piece of code? It is always important to provide this since there is a difference between what you "see" at the server side (the script) and what you have on the client side (the generated output). Commented Sep 1, 2012 at 19:56
  • @PeterSzymkowski What line break... Commented Sep 1, 2012 at 19:57
  • @CJSculti check agreco's answer Commented Sep 1, 2012 at 20:01

2 Answers 2

2

Remove linebreak:

$link="<script type='text/javascript'>window.open('http://www.fonefinder.net/findome.php?npa=$num1&nxx=$num2&thoublock=$num3&usaquerytype=Search+by+Number&cityname=')</script>";
echo $link;

Also, you should wrap the variables in braces: ${num1}, otherwise they may be interpolated incorrectly.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it just does absolutley nothing, as it does not redirect, the way I have it now, with it echoing the script.
I'd also point out that this will break if the PHP variables include apostrophes or any of a number of other things - You need to first URL encode the parameters urlencode() and then escape them to be used inside a JS String (replace ' and `\` and probably some more). Of course, if you can guarantee it will only ever be a number, this shouldn't be required
1
$link="<script type='text/javascript'>window.open('http://www.fonefinder.net/findome.php?npa=".$num1."&nxx=".$num2."&thoublock=".$num3."&usaquerytype=Search+by+Number&cityname=')</script>";
echo $link;

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.