cucuru
cucuru

Reputation: 3698

Submit form and close window

I can't understand why this isn't working.

I have a form opened in new tab, which I want to close when submitting:

<form name="form" id="form"  method="post" enctype="multipart/form-data" >
    //form inputs
    <button accesskey="C" class="boton" onclick="form.submit(); alert('waiting...'); window.close()"> <u>A</u>djuntar</button>

 </form>

when I remove window.close() the form is submitted, but when it's in my code, it shows the alert but not submitting.

Is there anything wrong?

Upvotes: 1

Views: 10621

Answers (3)

shayuna
shayuna

Reputation: 484

you don't have to open the form in a new window (or tab). you can either submit the data in ajax or use an inner hidden iframe as the target property of the form element. this way the form will open in a hidden inner element. e.g.:

<form id="eFrm" target="ifrm1">
</form>
<iframe id="ifrm1" name="ifrm1" style="display:none"></iframe>

Upvotes: 1

Dan
Dan

Reputation: 574

You probably already found a solution, but I am posting this anyway for anybody who comes across the same in the future.

I am using Java servlets, but I think that is the same with every form submission => ability to choose which page must be displayed after the posted data was processed.

Once I have submitted the form, the doPost method in the servlet allows me to say which URL I want the browser to display after the data has been processed.

So, you can do probably do something along these lines:

   request.getRequestDispatcher ( "/ClosePopup.html" ).forward ( request, response );

Then also create a file called ClosePopup.html, with nothing but a close () instruction

<!DOCTYPE html>
<html> 
    <head>
        <meta charset="UTF-8">
    </head>
    <body onLoad = "window.close();">
    </body>
</html>

This way it won't be the button you click to trigger the closure of the popup, but you will be loading a self destroying page after the form is submitted. The final result is identical.

Upvotes: 0

Quentin
Quentin

Reputation: 943185

The form won't be submitted until the event handler function has finished running.

alert blocks all execution of everything until the dialog is dismissed.

close closes a the window, so there is nowhere to load the form submission into, which cancels the request.


If you are going to close a window, don't expect it to be able to send an HTTP request at the same time.

Upvotes: 0

Related Questions