How to close Browser Tab After Submitting a Form?

Window.close( ) does not work as it used to. Can be seen here:

window.close and self.close do not close the window in Chrome

In my case, I realized that I didn't need to close the page. So you can redirect the user to another page with:

window.location.replace("https://stackoverflow.com/");

Remove onsubmit from the form tag. Change this:

<input type="submit" value="submit" />

To:

<input type="submit" value="submit" name='btnSub' />

And write this:

if(isset($_POST['btnSub']))
    echo "<script>window.close();</script>";

If you have to use the same page as the action, you cannot use onSubmit="window.close();" as it will close the window before the response is received. You have to dinamycally output a JS snippet that closes the window after the SQL data is processed. It would however be far more elegant to use another page as the form action.


<?php    
    /* ... SQL EXECUTION TO UPDATE DB ... */

    echo "<script>window.close();</script>";
?>

and Remove the window.close() from the form onsubmit event

Tags:

Javascript

Php