0

I am trying to submit a simple form that is in a pop-up dialog and then close the dialog. The best article I've seen is Submit a form in a popup, and then close the popup but it seems to work intermittently for me. The page launched by the form is a PHP page that modifies a record in the database. I thought that once the request is sent the PHP page will execute, even if the launching window is closed. Apparently not though. Sometimes the table is updated, sometimes it isn't. It seems like if the SQL operation isn't fast enough the page will be closed and the process is killed.

Here's the code:

<form id="xlationform" action="updatexlation.php" method="post" onsubmit="return closeForm(this);">
    Source: <br>
    <textarea disabled name="sterm" rows=10 cols=50><?php echo $source ?></textarea><br><br>
    Translation: <br>
    <textarea  name="xlt" rows=10 cols=50><?php echo $xlation ?></textarea><br><br>
    <input type="hidden" name="id" value="<?php echo $termid ?>">
    <input type="submit" value="Update">
</form>
<script>
function closeForm(f) {
    f.submit();
    window.close();
}
</script>

What's the best way of working this out? I want the window to be closed but the DB operation needs to complete first and I don't want to query the DB again if possible. Thanks for your help.

Community
  • 1
  • 1
Paulo Hgo
  • 834
  • 1
  • 11
  • 26

2 Answers2

0

The response page should simply have window.close in the onload event.

Chet
  • 3,461
  • 1
  • 19
  • 24
0

Do it in updatexlation.php like :

$e = mysqli_query(...) if ($e) { echo "<script>window.close();</script>"; }

But as mentioned above, avoid using popups.

Stv
  • 496
  • 6
  • 16
  • This worked, thank you all. I may end up not using the pop-up after all but this solution works like a charm. – Paulo Hgo Oct 15 '15 at 03:05