简体   繁体   English

JavaScript中的PHP和window.close

[英]PHP and window.close in JavaScript

I have a problem. 我有个问题。 I have a page that when you click a button, a popup with a form is shown. 我有一个页面,当您单击按钮时,会显示一个带有表单的弹出窗口。 So, I complete some data and I submit. 因此,我完成了一些数据并提交了。 What I want to do is, to submit the form, close the form and refresh the parent page. 我想做的是,提交表单,关闭表单并刷新父页面。 I don't want to do it with AJAX. 我不想用AJAX来做。

The problem is that in my parent page I have to refresh content with the input information of the form. 问题是我必须在父页面中使用表单的输入信息刷新内容。

So when I refresh, sometimes the data is shown and sometimes not. 因此,当我刷新时,有时显示数据,有时不显示。 Do you know why this could happen? 你知道为什么会这样吗?

I just use onsubmit="refreshParent()" in my form. 我只是在表单中使用onsubmit="refreshParent()" The info is stored always in my database, so I think the problem may be that sometimes the refresh catches the new info and sometimes not. 信息总是存储在我的数据库中,所以我认为问题可能在于有时刷新会捕获新信息,而有时却没有。

function refreshParent() {    
    window.opener.location.reload();
    window.close();
}

I use this to reload the page that opened a popup window: 我用它来重新加载打开了弹出窗口的页面:

 <script language="JavaScript">
 <!--
 function reloadParentPage() {
     window.opener.location.href = window.opener.location.href;
     if (window.opener.progressWindow) {
         window.opener.progressWindow.close()
     }
     window.close();
 }
 //-->
 </script>

By the way, the code above is called by a link or button in the popup page. 顺便说一句,上面的代码由弹出页面中的链接或按钮调用。

You have a race condition between the script doing the insert and the script reloading the parent. 在执行插入的脚本与重新加载父脚本的脚本之间存在竞争条件。

The solution is to call refreshParent on the page after the submit - that way you know the data is in the database. 解决方案是在提交后在页面上调用refreshParent-这样您就可以知道数据在数据库中。 You don't even have to do it on document ready - return a stub page that just defines and calls refreshParent in the head tag. 您甚至不必在准备就绪的文档上执行此操作-返回一个存根页面,该页面仅定义并在head标签中调用refreshParent。

In PHP when you run post script, at the end, include this code : 在PHP中,当您运行发布脚本时,请最后包含以下代码:

echo '<html><script language="javascript">
      parent.location.href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'"; // or any other url
      </script></html>';

This will output a javascript that will reload the windows. 这将输出一个将重新加载Windows的JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM