Click to See Complete Forum and Search --> : Form submission & close window


netfrugal
06-30-2007, 06:14 PM
I know that I can use javascript on this one, but I ran into a problem.

I have a simple form that submits data to my database - easy stuff. But, I want to close the popup window after submission.

Here's what I've done:



<form id="form1" name="form1" method="post" action="" onSubmit="window.close();">

<input type="text" name="email" value="1" />
<input type="submit" name="Submit" value="update" class="textbox"/>
</form>


I have the INSERT statement on the same page, but I think the onSubmit="window.close();" stops the form data from reaching the insertion script.

I tried putting the data inside a separate file too, but I still got the same results, the data does not reach the INSERT statement.

How can I close the browser window after the data has been processed?

thanks!

big.nerd
07-01-2007, 02:29 AM
netfrugal,

Put the window.close on the <body onload of the php file that processes whatever you are doing...

MarkR
07-01-2007, 05:49 AM
Can I clarify big.nerd's response a bit:

- You have to close the window on the output from the form.
- Using body onload will stop it from closing the window before the document is completely loaded.
- You might want to put a message "Thanks, the form is processed, you may close this window" in case they have JS disabled.

Don't close the window in onsubmit, otherwise it will close it before it submits, hence the data will never reach the server.

Mark

JPnyc
07-01-2007, 10:08 AM
onSubmit="setTimeout('window.close()',2000)"

MarkR
07-01-2007, 11:42 AM
I doubt whether using setTimeout would work in onsubmit, because as a new page would (probably) be loaded after submit, any timeouts set by the previous page would be (implicitly) cleared.

This is just a guess though, so if you've tested it on all commonly used browsers and it works fine on them all, great.

Mark

JPnyc
07-01-2007, 04:03 PM
I haven't tested but i dont see any reason why it wouldn't call the function delayed. I assumed the form being submitted was on the window he wants to close.

big.nerd
07-02-2007, 12:26 AM
If I am not mistaken, the onsubmit will process before the form actually submits, whereas it will send the request to the server and wait for a response.

For instance, I always put my javascript validation in the onsubmit (not on the submit button), in addition to server scripted, before anyone mentions it.