Submit HTML form to pop up window

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Posted in HTML, JavaScript.

5 Responses to “Submit HTML form to pop up window”

  1. Rahat BashirNo Gravatar Says:

    this is nice and useful functionality

  2. pro_romanNo Gravatar Says:

    nice technique, man.

  3. Mohammad Sajjad HossainNo Gravatar Says:

    Nice trick.

  4. Anis Uddin AhmadNo Gravatar Says:

    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.

  5. cwxwwwxwwxwxNo Gravatar Says:

    well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

Leave a Reply