Html help

Started by Vidar, November 05, 2008, 10:34:21 AM

Previous topic - Next topic

Vidar

I have a website with a pop-up window that has a form in it. Can I make a button that submits the form, and closes the window?
Using onsubmit="javascript:window.close();" doesn't work. The window is closed, but the form data is lost.
\^.^/ \O.O/ \¬.¬/ \O.^/ \o.o/ \-.-/' \O.o/ \0.0/ \>.</

Mao

document.<form>.submit(); window.close();

Maybe make a function for that?  I'm not sure.  I'd have to test it as this is just off the top of my head.

RobbieThe1st

The problem is you are trying to trigger it at the same time or before you fully submit the page. What you want to do is have your closing code on on the response from the server:
So, say your form starts out like:
<FORM name="formname" ACTION="some_page.php" METHOD="POST">

The form data will be submitted to some_page.php as POST data, correct?
What you simply need to do is have some_page.php send closing code. What you want it to do is immediately close the window after submitting, so in some_page.php, simply have it send something like this:

<html><head><title>Nothing</title></head><body onload="window.close();"></body></html>

This code will, as soon as the page loads, run the "onload" JS snippit, which closes the window(Or, should in any case - you may have to change the code or find new code ["top.close();" may also work).

Integrating it into your PHP page wise, you could place it right at the beginning of the page, before the <?php, which would work fine, or you could echo it anywhere in your page.
This means also that, if you need to, you can have your php page close it if certain input conditions are met, and not if others(say, if one or more fields are not valid, you could output the origional page with whatever values were sent, giving the user a chance to correct the input)

I hope I didn't make it sound too complicated; I have a tendency to do such things.


-RobbieThe1st

Pasteris.ttf <- Pasteris is the font used for text in DMFA.

Vidar

Thanks, Robbie. That one works perfectly.

Mowser, a valliant effort to be sure, but that one doesn't work either. If only things would be that simple...
Thanks anyway.
\^.^/ \O.O/ \¬.¬/ \O.^/ \o.o/ \-.-/' \O.o/ \0.0/ \>.</

Mao

Eh, I don't use standard submit buttons.  I always use a  button that calls js on click, do my final processing, submit and close.

Jack McSlay

#5
1 - document.<form> is deprecated. document.getElementById(string id) and document.getElementsByTagName(string tagName) are preffered.
2 - I advise against this behavior. having the form window dissappear right upon submission might confuse the user into thinking there was a crash or error that caused the window to close. Have an error/success message on the window itself after the form is submitted and put a button and/or timer to close the window, if desired.
edit: 3 - In fact, unless there's an extreme requirement about having the form in a new window, you should just put it in the default window. A lot of people hate having forms in separate windows popping up on a website, and there's often the probability of your popup form being blocked by the browser's anti-popup feature.
Keyboard not detected. Press F1 to resume.

Mao

The problem with those points, Jack, is that clients often don't care what is 'best practice'. (a concept I don't really believe in because it's almost completely arbitrary in most cases concerning form and flow of web pages).

As for document.<form> being deprecated that is true, but if you're supporting a legacy system it's best to keep it consistent.  Another item against that, I don't see the problem with calling a forms submit method directly.  It's much more clear what you're doing if you use document.(form[n] || <formName>).submit() (imho, getting back to that completely arbitrary side of form and format) than say going document.getElementById(<formName>).submit().