PHP Javascript: Closing Popup calling parent window in background - javascript

Let's say a parent window is www.mysite.com/index.php has several links. When user clicks on these links, a popup is opened.
The popup has some forms. When the user clicks submit button, another script is called within the popup like this:
if ((isset($_POST['Complete'])) && ($_POST['Complete']=='Complete')) {
unset($_POST['Complete']);
include 'result.php';
return;
}
The result.php enters the data into database and pass the result to parent window with the following
<script language="JavaScript" type="text/javascript">
window.opener.location.href = 'submission.php';
window.close();
</script>
The parent window then show the final result, update the master database and emails the users.
Now here is the problem.
User clicks the link on parents.
Form opens, but user click close (browser cross button).
The user then closes parent window
The user then visit some other page of the site; say other.php
But the master database is getting updated and the user is getting the email (All null values though).
Since the form was never completed, the result.php should never be called. and if result.php is never called, submission.php should never be called.
What is happening here?

Change the type of the close button to button.

Related

How to update the value of a column in my table when the current tab close or navigate to another page?

I have a page and I named it to mypage.php. I have a table MyTable with a column STATUS. I want to update the status to T when the user enters to the page and change to F when the user leaves or close from the page.
I tried with onbeforeunload with JS. Help me any way.
Try
window.onunload=function(){
/......when user closes..../
}
window.onload=function(){
/....when user enters page.../}
I think you should try the "onfocusout" or "blur" event if you want to update it when the user goes on another tab and not only when he close your tab. Put the onfocusout on the body tag and it will fire when the user will exit your tab, by switching to another tab or by closing yours.

Preempt moving to another page with a pop-up

Let's say I have an update page in HTML5 and if the user edited a field, I want to show a pop-up whenever the user will exit the page (by clicking on another link) without clicking the update button. The pop-up will confirm if the user wants to save his edits or if the user declined, simply continue on the link he clicked before. How to achieve this in HTML5/JavaScript? Is there any function that preempts redirects? Thanks.
Use the onbeforeunload event.
Check out the demo: onbeforeunload Demo
JS code:
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>

How to submit parent window form when child window closes

I have a gridview displayed in a page which also has a link. When I click on the link, a showModalDialog() will open up where I can edit the value.
After that when pressing Update button the value is getting updated in the database and my child form closes perfectly.
But I need to update my parent page table as well. So that the new value gets reflected in the parent form. I tried the following in the child window.
<body onunload="window.opener.document.forms[0].submit();">
But that is not working. Suggest me a good solution.
You do not have onUnload in a modal dialog
var res = showModalDialog(...); // execution is blocked until modal is closed
if (res) location.reload(1); // res contains what dialog set returnValue to
and in modal dialog you do
window.returnValue=true; // if ok submission
window.close(); // will return control to opening window
Anyway, did you look to the right? there are many duplicate questions about gridviews and modal dialogues
I guess you have a function on Update button click, which updates the database and close the form, can you place that code (the one that you have in unload) in that function before closing the window

How to handle when the x button is click on pop-up web page using Javascript?

I have a “pop-up” web page inherits from System.Web.UI.Page in a plug-in which is a part of a large web framework. The markup is inside <asp:Content> below. I need to perform some actions (updating the page behind) when the user clicks on “Close” button (id = _close) and when the user clicks on the ‘x’ button on the right corner of the page. My page is not displayed in the browser. I got the “onclick” (in the code behind) to work for “Close” button below, but I don’t know how to detect when the ‘x’ button is clicked. I tried on window.beforeunload and window.onclose, it doesn’t work. “Hello World” pops up when my web page opens and when I click “Close” (_close) button.
How do I handle for the click on the ‘x’ the same way when “_close” button is clicked?
<script language="javascript">
function doClose() { alert('Hello World!'); } window.onload = doClose(); // also tried with window.beforeunload </script>
You can execute the code on the opener. Write a method in opener and execute it from the pop-up window.
http://www.w3schools.com/jsref/prop_win_opener.asp

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

Categories