3

I have some JavaScript variable in the parent browser window and now I want to open a new window(an existing php file) by clicking a certain button and pass those variable to the opened page. I know I can achieve that by GET method and I just wonder if there is a clearer way I can do it so I don't have to append the variable value at the end of the URL. I also searched on google and SO, someone suggested using window.opener somehow it seemed not to apply to php. I'm still a newbie. Any help is appreciated. Thanks in advance.

scripts in the opener page

var newWindow = window.open ("myfile.php" ,"_blank");
newWindow.var1="hello";
newWindow.var2="world";

myfile.php

//Is it possible to use the variables in php page as below?
window.opener.var1;
window.opener.var2;
3
  • Research the $_POST array instead of $_GET to avoid appending data to the URL string. Also, before anyone will post answers, you need to post what efforts you've made (ie: where's your coded attempt?) Commented Jul 6, 2012 at 18:36
  • This question is too vague. What are you trying to do? Why is appending variables to the URL a problem? Commented Jul 6, 2012 at 18:42
  • @colonelclick, the variable passed could be really long so I would rather avoid appending it to the URL if that's possible Commented Jul 6, 2012 at 19:27

2 Answers 2

2

If you want those variables to be accessible to the PHP code, then no, the GET method is the easiest and most clear way of doing it.

No JS needed to open new window:
<a href="myfile.php?var1=foo&var2=bar" target="_blank">mylink</a>

JS method:
window.open("http://mysite.com/myfile.php?var1=foo&var2=bar");

myfile.php

<?php
    $var1 = $_GET['var1'];
    $var2 = $_GET['var2'];
    ...
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a form with POST method and target="_blank", but other than that your options are limited.

People don't like popups anyway.

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.