提交表单并关闭窗口——这是正确的做法

Submitting a form and closing the window - the correct way to do it?

本文关键字:窗口 提交 表单      更新时间:2023-09-26

我试图提交一个简单的表单,是在一个弹出对话框,然后关闭对话框。我所见过的最好的文章是在弹出框中提交表单,然后关闭弹出框,但它似乎间歇性地为我工作。表单启动的页面是一个PHP页面,用于修改数据库中的记录。我认为,一旦请求被发送,即使启动窗口被关闭,PHP页面也会执行。显然不是。表有时更新,有时不更新。似乎如果SQL操作不够快,页面将被关闭,进程将被终止。

代码如下:

<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>
解决这个问题的最好方法是什么?我希望窗口被关闭,但DB操作需要先完成,如果可能的话,我不想再次查询DB。谢谢你的帮助。

在updatexation .php中这样做:

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

但是正如上面提到的,避免使用弹出窗口

响应页应该只有一个窗口。关闭onload事件