<?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>
我想在提交表单后关闭页面。运行上述代码后,单击提交按钮后页面关闭,但SQL不执行。
有人能帮我吗?
<?php
/* ... SQL EXECUTION TO UPDATE DB ... */
echo "<script>window.close();</script>";
?>
onsubmit
事件中删除window.close()
将onsubmit
从form
标签中移除。改变这个:
<input type="submit" value="submit" />
:
<input type="submit" value="submit" name='btnSub' />
然后这样写:
if(isset($_POST['btnSub']))
echo "<script>window.close();</script>";
窗口。Close()不再像以前那样工作。可以在这里看到:
窗口。亲密和自我。关闭浏览器
在我的例子中,我意识到我不需要关闭页面。所以你可以用
将用户重定向到另一个页面window.location.replace("https://stackoverflow.com/");
如果要使用与操作相同的页面,则不能使用onSubmit="window.close();"
,因为它将在收到响应之前关闭窗口。必须直接输出一个JS片段,在处理完SQL数据后关闭窗口。然而,使用另一个页面作为表单操作会优雅得多。
try onsubmit="submit(); window.close()"
这对我来说非常有效,:
$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>';
}
这是因为事件onsubmit
是在表单提交之前触发的。
删除onSubmit
并在处理请求后将该JavaScript输出到PHP脚本中。您正在关闭窗口,并取消对服务器的请求。
你可以试试这个方法
window.open(location, '_self').close();