Submit HTML form to pop up window
September 22, 2008 — Arafat Rahman
Some times we may need to submit HTML form to a pop up window. Let’s take a look into the details.
Say for I have a HTML form.
<form name="contactForm" method="post" action="thank-you.php">
Name : <input type="text" name="name" id="name" /> <br />
E-mail : <input type="text" name="email" id="email" /> <br />
<input type="submit" value="Submit" />
</form>
We may submit this form to a blank window by setting target=”_blank”, but when we need to submit the form to a pop up window then we should do some extra code. Let’s try.
Step one: Defining a JavaScript function
function openWindow(url, wname, width, height) {
window.open(url, wname, "height=" + height + ",width=" + width + "location = 0, status = 1, resizable = 0, scrollbars=1, toolbar = 0");
return true;
}
Above function opens a pop up window according to the supplied arguments. There is a tricky thing. ‘name’ is a keyword in JavaScript. We should not put the variable name as ‘name’. IE reports error if we put name as a variable name.
Step two: Setting the form attributes
Now we set the form attributes like bellow
<form name="contactForm" method="post" action="thank-you.php" target="popupWin" onsubmit="return openWindow('thank-you.php', 'popupWin', 600, 500);">
Step three: Preparing thank you page
Now we are ready to prepare the thank-you.php page for processing the form data.
Some more things must follow for security and to protect spamming. We should apply both JavaScript and server side validation into the form, protect spamming and XSS.








September 25, 2008 at 9:44 am
this is nice and useful functionality
September 28, 2008 at 4:01 pm
nice technique, man.
November 5, 2008 at 10:22 am
Nice trick.
November 28, 2008 at 12:59 pm
Nice and useful post
Be careful about using “window.open” function. Bcoz popup blockers can disable opening a new window using it. And, most of the normal users (I mean except developers) use popup blocker and instruct it to block all popups without knowing what is he doing actually.
Better u can use “_blank” as value of target attribute of your form to submit the form to a new window and it’s safe from blockers. Then, if u want to resize the window, u can do it in onLoad event of that new window.
December 23, 2008 at 1:02 pm
well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch