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

<form method  = "post"
      action  = "<?=$_SERVER['php_self']?>" 
      onSubmit= "window.close();">
  ...
  <input type="submit" value="submit" />
  <input type="reset"  value="reset" />
</form>

I would like to close page after submitting the form. After running the above code the page is closed after clicking submit button, but the SQL doesn't execute.

Can anyone help me?

starball
  • 20,030
  • 7
  • 43
  • 238
Acubi
  • 2,793
  • 11
  • 41
  • 54

8 Answers8

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

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

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

hakre
  • 193,403
  • 52
  • 435
  • 836
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • maybe change it to `echo "` – HighTechProgramming15 Feb 13 '17 at 17:14
  • That **did** work form me, but it wasn't because i was using the `header('Location: text.php');` function before i'm trying to close it, so in case you are using a `header` but the `window.close()` script in the PHP page that you are redirecting to... (`text.php` in my example)... Hope this helps. – Mousa Alfhaily Dec 23 '17 at 16:27
  • 7
    year 2018: doesnt work anymore in chrome or firefox - some security issues – The Vojtisek May 31 '18 at 06:24
11

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>";
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Sonal Khunt
  • 1,876
  • 12
  • 20
5

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/");
Community
  • 1
  • 1
blueseal
  • 355
  • 3
  • 5
  • 15
1

This worked brilliantly for me, :

 $query = "INSERT INTO `table` (...or put your preferred sql stuff)" 
 $result = mysqli_query($connect, $query); 
 if($result){
  // If everything runs fine with your sql query you will see a message and then the window
  //closes
        echo '<script language="javascript">';
        echo 'alert("Successful!")';
        echo '</script>';
        echo "<script>window.close();</script>";
        }
 else {
        echo '<script language="javascript">';
        echo 'alert("Problem with database or something!")';
        echo '</script>';           
        }
Macarrao
  • 60
  • 1
  • 11
  • 1
    This still doesn't solve the problem imo. I am trying to build a small registration-system for larger events. The registration happens by scanning QR-codes ("tickets"), and I am trying to minimalize amounts of clicks. The alert will still require my registration team to do this extra interaction (confirm/ok the alert)! – MahNas92 Sep 21 '16 at 01:33
1

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.

Viruzzo
  • 3,025
  • 13
  • 13
1

try onsubmit="submit(); window.close()"

Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45
0

That's because the event onsubmit is triggered before the form is submitted.

Remove your onSubmit and output that JavaScript in your PHP script after you have processed the request. You are closing the window right now, and cancelling the request to your server.

Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
-1

You can try this methods

window.open(location, '_self').close();
warfish
  • 613
  • 5
  • 20