Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General Web Development >  Submitting a FORM to a new window
Add to MyHood
   Submitting a FORM to a new window   [ printer friendly ]
Stats
  Rating: 4 out of 5 by 11 users
  Submitted: 08/12/02
Matthew Hildebrand ()

 
I recently saw a posting asking "How can I force the results of a form submittal to open up in a new window?" so I thought that I would write up this tutorial to explain the process.

First, start off by building your form just like you would normally. Here is an example form:

<form name="formName" method="get" action="myFile.cgi">
  <input type="text" name="fieldSearch">
  <input type="submit" name="clickSubmit" value="Search">
</form>
Now, this form will just turn around and submit back to the same window, which we don't want it to do. The first thing that we have to do to prevent this is to get the form to actually call a javascript function, which we will call submitForm(). To do this, we will change our submit button into a regular button and add an onClick property which calls the function submitForm() and passes the name of the form as such:

<input type="button" name="clickSubmit" onClick="submitForm(document.formName)" value="submit">
Now the fun part, building the javascript function. Here is the basic code for the function:

<script type="text/javascript">
<!-- // hide from older browsers
  function submitForm(theForm) {

  } // function submitForm(formName)
// -->
</script>
This code set up the shell for the function. Within the submitForm() function, we need to establish a new window by including the following two lines:

myWindow = windows.open(windowURL, windowName);
myWindow.focus();
The first line will create the new window with a name of windowName and open it to the URL defined by windowURL. Now, obviously, we haven't created these two variables yet, so let's do so. The windowName variable is fairly easy as we can see here:

windowName = "myWindow\'s name";
The windowURL variable is a little more complicated however. We have to basically build the entire URL from scratch based on the information that is in the form. Here is one example of how to do this:

windowURL = theForm.action + '?';  // start with the file that the form is supposed to post to, and prepare to append form data after it
After you create the base, you need to append to it each field that is in the form, for pretty much all field types, the following code will work, although there are a few that are kinda funky (I'll let you find those on your own).

windowURL .= 'fieldSearch=' + theForm.fieldSearch.value + '&';
Repeat this code for each field in the form, and you should be set to go. The final code should look something like this:

<script type="text/javascript">
<!-- // hide from older browsers
  function submitForm(theForm) {
    windowName = "myWindow\'s name";
    windowURL = theForm.action + '?';
    windowURL .= 'fieldSearch=' + theForm.fieldSearch.value + '&';
    myWindow = windows.open(windowURL, windowName);
    myWindow.focus();
  } // function submitForm(formName)
// -->
</script>

<form name="formName" method="get" action="myFile.cgi">
  <input type="text" name="fieldSearch">
  <input type="button" name="clickSubmit" onClick="submitForm(document.formName)" value="submit">
</form>
As a side note, if you try this just on your local drive or something, the myFile.cgi file MUST exist in the same path as the file containing the code otherwise the variables aren't displayed correctly after the filename (but they are when the file is there... go figure).

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
Or you could just do:

<form action="script.cgi" method="post" target="_new">
<!-- ... -->
</form>
-- Heath Stewart, August 19, 2002
 
Boy, now I feel silly. For some reason I wasn't able to get that snipit that you posted to be able to work, so I decided to do it the hard way. Oh well, it was a good learning experience, that's for sure.
-- Matthew Hildebrand, August 20, 2002
 
Good idea though.. =)
-- Larry Mak, August 20, 2002
 
Well, I suppose that it would be useful however if you were trying to do some javascript form validation to eleminate trips to the server and back.
-- Matthew Hildebrand, August 20, 2002
 
I still learned something, thanks.
-- Brent Bishop, August 28, 2002
 
Thanks for the tutorial and especially the comments that show the best way to do it.
-- Beau Neal, August 28, 2002
 
using the target attribute is ok - but with this script, you can specify the window (size, toolbars, etc) by adding the config stuff in the "myWindow" definition:

myWindow = window.open(windowURL, windowName, 'width=455,height=450');

i was having fits trying to accomplish this, thanks for 99.9% of the solution :)
-- C Burt, August 30, 2002
 
Good point, you can't specify window details using the target method. Maybe this is good for something after all. :)
-- Matthew Hildebrand, September 10, 2002
 
This might be a bit more modular:

function submitForm(f) {

w = window.open(f.action, "WINDOW-NAME");
w.focus();
f.submit();
}

and in your form...

<form name="formName" action="YOUR-URL" method="GET" target="WINDOW-NAME">
contents of your form...
<input type="button" onClick="submitForm(document.forms.formName);">
</form>

This would submit the entire form into that new window - as long as your target & window name are the same. This work on IE, haven't tested it on other browsers, but I imagine it would work.

-- Sean Clarke, October 04, 2002
 
I would like to thank Sean Clarke for his snippit...it made my headache go away. Cheers.
-- Jeff Busby, October 07, 2002
 
Before you post a tutorial you really should double check your code. Little things like " something .= this" prevent novice users from understanding what your trying to teach. Here are two errors I found in our friends syntax:

window.open(windowURL, windowName) // NOT windows.open
windowURL .= // Remove the period

Hope this helps a little, other than the two syntax errors I found this tutorial very helpful.

Thank you,

-Aaron J. Greenlee
-- Aaron Greenlee, October 14, 2002
 
Thanks for posting those errors Aaron. If we are ever able to edit our tutorials, I'll make sure to update it :)
-- Matthew Hildebrand, October 16, 2002
 
Copyright © 2001 DevHood® All Rights Reserved