Passing a POST variable to a new window

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Hey guys,

Is there anyway or passing a POST form variable to a new window? Tried googling for it but my googling skills suck...

Haven't played around with Javascript for a while, can it even be done?

Can't give a link because it's an internal site I'm working on.

calculator's picture

They have: 40 posts

Joined: Nov 2007

Hi Renegade,

You can use a server side script (PHP, ASP etc...) to retrieve data passed from a 'post' method.

On page 1:

<form action="page2.php" method="post">
<p><label for="name">Name:</label><input type="text" name="name" id="name" /></p>
</form>
'

On page 2 before the Doctype:

<?php
if(isset($_POST['name'])){
  
$myFromVar = $_POST['name'];
}
?>
'

and wherever you want the data to appear on page 2:
<?php echo $myFromVar ; ?>'

You should also check that the data that is passed is of the right format by 'cleaning' it. Ie. if you have a drop down menu called 'color' with only 3 values (blue, red, green), I would do something like:

<?php
if(isset($_POST['color'])){
     switch(
$_POST['color']){
          case
'blue':
          case
'red':
          case
'green:
                $myColor = $_POST['
color'];
                break;
          default:
                exit('
Error');
      }
}
?>
'

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Oh yes, that's about what I did but, the new window is not picking up anything:

This is more or less what I have at the moment:
tools.php

<form action="javascript: fnOpenWin('redirect.php?action=connectionhistory');" method="post">
username: <input type="text" name="username" /><br />
<input type="submit" value="search" />
</form>
'

js.js

function fnOpenWin(url) {
window.open(url);
}
'

redirect.php

if($_GET['action'] == "connectionhistory") {
$url = "http://www.example.com/tools.php?username=" .  $_POST['username'];
}
'

Doing an echo of $url just gives me "http://www.example.com/tools.php?username=" so for some reason, the post variable is not being passed.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

If you already Googled it and didn't find any help, it may not be possible. The browser is probably not posting the information because you have a script in it's place, and not a web page.

Some alternatives:

  1. Send the username in the URL as a $_GET field.
  2. If you have other fields other than a username, submit the form to a PHP file using AJAX, make that script store the $_POST data to the $_SESSION, open the popup, and then retrieve that data from the $_SESSION.
calculator's picture

They have: 40 posts

Joined: Nov 2007

Hi Renegade,

How about:

tools.php

<form action="redirect.php?action=connectionhistory" method="post" target="_blank">
'

or xhtml strict compliant:
in the separate js.js file

window.onload = function() {
if(document.getElementById('myform')){
var myform = document.getElementById("myform");
myform.target= "_blank";
}
}//end window.onload
'
and in tools.php
<form action="redirect.php?action=connectionhistory" method="post" id="myform">'

You could also make the target ="_blank" a window.open with sizing capabilities quite easily by passing the href var to js if you need to.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Oh cool, thanks for that Calculator, I solved it by using target="_blank" didn't even know you could do that with forms... Smiling

calculator's picture

They have: 40 posts

Joined: Nov 2007

No worries, glad I could help Wink

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.