Javascript 提交表单后如何关闭浏览器选项卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8355316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:39:31  来源:igfitidea点击:

How to close Browser Tab After Submitting a Form?

phpjavascript

提问by Acubi

<?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.

我想在提交表格后关闭页面。运行上述代码后,单击提交按钮后页面关闭,但 SQL 不执行。

Can anyone help me? Thanks in advance!

谁能帮我?提前致谢!

回答by Shakti Singh

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

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

and Remove the window.close()from the form onsubmitevent

window.close()从表单onsubmit事件中删除

回答by Sonal Khunt

Remove onsubmitfrom the formtag. Change this:

onsubmitform标签中删除。改变这个:

<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>";

回答by blueseal

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

Window.close() 不像以前那样工作了。可以在这里看到:

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

window.close 和 self.close 不会关闭 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/");

回答by Marijn van Vliet

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

尝试 onsubmit="submit(); window.close()"

回答by Viruzzo

If you haveto 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.

如果您必须使用与操作相同的页面,则不能使用,onSubmit="window.close();"因为它会在收到响应之前关闭窗口。您必须在处理 SQL 数据后以动态方式输出关闭窗口的 JS 代码段。然而,使用另一个页面作为表单操作会优雅得多。

回答by Jan Han?i?

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

那是因为该事件onsubmit是在提交表单之前触发的。

Remove your onSubmitand 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.

onSubmit处理完请求后,删除并在 PHP 脚本中输出该 JavaScript。您现在正在关闭窗口,并取消对您的服务器的请求。

回答by Macarrao

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>';           
        }

回答by warfish

You can try this methods

你可以试试这个方法

window.open(location, '_self').close();