Using Javascript to open a new window with form values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Using Javascript to open a new window with form values

Post by Grim... »

Okay, here's the deal.

I'd like to have a button on my page that opens a new 400x400(ish) window, and sends the contents of a textarea (called 'varText' for simplicity) to this new window without closing the current page (so the page can be submitted normally later).

On this new page, I then need to get the contents of varText into a PHP string :?

I have trawled the web, and the only vaugue help I could find is on Experts Exchange here, but I'm not a member so I can't see their answer.

I know I could just get the javascript to build a link like 'newpage.php?varText=contents of text area', but it's possible that the text area will have all sorts of nasty &, ', ['s etc.

That's about it, except for: Hello again, I've been away for ages :)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

How To Send A Javascript Variable To A PHP Variable

Post by Grim... »

Okay, I've solved part of it for myself.
Incase it helps others, here is how to get a PHP variable from a Javascript variable:

Code: Select all

<html>
<head>
  <title> bogus </title>
  <script language='JavaScript'>
  <!--
    var myVal = 'happy, happy!';

    function getvariable(val) {
      var dummy = eval(val);
      document.write(dummy);
    }
  // -->
  </script>
  <?php
    function get_JS_var($js_var_name) {
      $x = &quote;<script> getvariable('&quote; . $js_var_name . &quote;'); </script>&quote;;
      return $x;
    }
  ?>
</head>
<body>
  <form name='myForm' action='javascript:void(null)'>
    <?php
      $abc = get_JS_var(&quote;document.forms&#1111;0].name&quote;);
      $def = get_JS_var(&quote;myVal&quote;);
    ?>
    <center><?php print &quote;abc: &quote; . $abc; ?></center><br>
    <center><?php print &quote;def: &quote; . $def; ?></center><br>
  </form>
</body>
</html>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Now, does any handy Javascript person know how to send to varText variable from the first page to a popup page without affecting the first page?

I'm going to use cookies for now, but I'm sure there is a more elegant way of doing things.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You could try using a hidden form field and submitting the data using POST.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Wouldn't that submit the first page though?

To make things simpler, I'll explain what I'm doing.

On a forum's 'New Topic' page (called newtopic.php), there will be a button marked 'Spell Checker'.
When someone presses the button, a new window will open with the contents of the textarea varText from newtopic.php, and all the spellcheckery gubbins.
When they spellchecking has finished, the user clicks 'finished' on the spell checker pop-up window and the corrected text is placed back into varText on the newtopic.php page, ready to be submitted.

I've done everything else, but I can't get the contents of varText to the popup page!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Re: How To Send A Javascript Variable To A PHP Variable

Post by Grim... »

Grim... wrote:Okay, I've solved part of it for myself.
Incase it helps others, here is how to get a PHP variable from a Javascript variable:
Bums, no I haven't.
That code just inserts the javascript into a variable, so when you print it out the browser converts the javascript for you.
It can't be done, can it?
Arse.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Done it!

Post by Grim... »

No, wait, finally done it.

This isn't the most elegant way of doing things, but, hey - it works.

Okay, to get a javascript varible into a php variable:

1. Create a page with just head, body, and a form with hidden fields for whatever variables you want. Set the form attributes to point it at a seperate php page

Code: Select all

<form method=&quote;post&quote; action=&quote;myphppage.php&quote;>
2. In the head, put the JS code that gets the variables you need.

3. In the body, put an onload handler like the following:

Code: Select all

<body onLoad=&quote;document.forms&#1111;0].hiddenFieldName.value = getvariable('myJSVariable');&quote;>
4. You can add several of these into one onload event, as long as they are seperated by semi-colons(;), thusly:

Code: Select all

<body onLoad=&quote;document.forms&#1111;0].hiddenFieldName.value = getvariable('myJSVariable'); document.forms&#1111;0].hiddenFieldNameB.value = getvariable('myJSVariableB');&quote;>
5. Now, at the end of the onload event(before the last quote), add

Code: Select all

document.forms&#1111;0].submit();
6. That's it!


Here is a complete example page; lets say you wanted to send the users screen resolution to PHP...

Code: Select all

<html>
<head>
<script language=&quote;javascript&quote;>
var width = screen.width;
var height = screen.height;
</script>
</head>
<body onload=&quote;document.forms&#1111;0].screenWidth.value = getvariable('width'); document.forms&#1111;0].screenHeight.value = getvariable('height'); document.forms&#1111;0].submit();&quote;>
<form method=&quote;post&quote; action=&quote;nextpage.php&quote;>
<input type=&quote;hidden&quote; name=&quote;screenWidth&quote; value=&quote;&quote;>
<input type=&quote;hidden&quote; name=&quote;screenHeight&quote; value=&quote;&quote;>
</form>
</body>
</html>
From here, it should be pretty easy when you get to 'nextpage.php' to start manipulating the variables in PHP.

Phew!
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Use JavaScript to transfer the contents of the JavaScript variable, not PHP. I'm not sure how to do it, but I know it can be done. You'd have to repost the page if you did it with PHP. You said before you weren't wanting to do that.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

you can finish tidying this up but the basis work.
the first page is the main page so run it

Code: Select all

<html>
<head>
<title>message window
</title>
<script type=&quote;text/javascript&quote;>
function checkSpelling()
{
   window.open(&quote;./newWindow.html&quote;, &quote;&quote;);
   	
}
</script>
</head>
<body>
<textarea id=&quote;message&quote;>
</textarea>
<input type=&quote;button&quote; name=&quote;spellcheck&quote; id=&quote;spellcheck&quote; value=&quote;spell check&quote; onclick=&quote;checkSpelling()&quote; />
</body>
</html>
then it will opens this up and retrieve the text from the textarea on the first page and when finished send the text back down and close the window. I called this one newWindow.html.

Code: Select all

<html>
<head>
<title>
</title>
<script type=&quote;text/javascript&quote;>
function finished()
{
   window.opener.document.getElementById('message').value = document.getElementById('message').value;
   window.close();
   	
}
function getMessage()
{
  document.getElementById('message').value = window.opener.document.getElementById('message').value
}
</script>
</head>
<body onload=&quote;getMessage()&quote;>
<textarea id=&quote;message&quote;>
</textarea>
<input type=&quote;button&quote; name=&quote;spellcheck&quote; id=&quote;spellcheck&quote; value=&quote;finished&quote; onclick=&quote;finished()&quote; />
</body>
</html>
its probably the javascript your after anyway.

phpScott
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Ah, that's much better than using a cookie.

Thanks!
Post Reply