PHP and window.close in JavaScript

此生再无相见时 提交于 2019-12-25 01:56:15

问题


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.

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. 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();
}

回答1:


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.




回答2:


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




回答3:


In PHP when you run post script, at the end, include this code :

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.



来源:https://stackoverflow.com/questions/8229230/php-and-window-close-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!