Page 1 of 1

Using Javascript to open a new window with form values

Posted: Wed May 11, 2005 7:57 am
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 :)

How To Send A Javascript Variable To A PHP Variable

Posted: Wed May 11, 2005 9:17 am
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>

Posted: Wed May 11, 2005 9:20 am
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.

Posted: Wed May 11, 2005 9:22 am
by Chris Corbyn
You could try using a hidden form field and submitting the data using POST.

Posted: Wed May 11, 2005 9:26 am
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!

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

Posted: Wed May 11, 2005 7:37 pm
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.

Done it!

Posted: Wed May 11, 2005 7:59 pm
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!

Posted: Wed May 11, 2005 8:06 pm
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.

Posted: Thu May 12, 2005 6:19 am
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

Posted: Thu May 12, 2005 7:59 am
by Grim...
Ah, that's much better than using a cookie.

Thanks!