This topic is locked

Open Javascript Popup Window then automatically close it after saving

5/12/2011 11:58:10 AM
PHPRunner Tips and Tricks
F
FunkDaddy author

Step 1.

Add the following code via a "insert php snippet" to the page where you want to include a link (or button if you choose) to open a new popup window.



echo "

<!-- Codes by Quackit.com -->

<script type=\"text/javascript\">

// Popup window code

function newPopup(url) {

popupWindow = window.open(

url,'popUpWindow','height=400,width=530,left=20,top=20,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')

}

</script>

<a href=\"JavaScript:newPopup('http://localhost/your_other_application_form_add.php';);\">Add New Comment</a>";


Step 2.

In the page that gets opened inside the popup window, you add this code to Javascript OnLoad



$("#submit2").bind("click", {page: this}, function(e){

var page = e.data.page;

page.on('beforeSave', function(formObj, fieldControlsArr, pageObj){

formObj.baseParams['submit2'] = 'Save and back to list';

}, page, {single: true});

page.on('afterSave', function(respObj, formObj, fieldControlsArr, pageObj){

delete formObj.baseParams['submit2'];

}, page, {single: true});

page.saveHn(e);

});


Step 3.

Add a new button (or simply replace existing save button) on that popup form to "save and close" such as:



<INPUT class=button id=submit2 type=button value="Save And Close" name=submit2>


Step 4.

Now, in the same popup form go to the event "After Record Added" (or if editing find the event counterpart) and add this:



if ($_REQUEST["submit2"]=="Save And Close")

{

echo "<script type=\"text/javascript\">window.close();</script>";

exit();

}


That is it. Now your popup will automatically save and close.
Special Note: I want to acknowledge that steps 2 through 4 were adapted (99% of it) from Jane's entry here: http://www.asprunner.com/forums/topic/10748-adding-an-additional-button-to-the-edit-page/
Cheers,

F
FunkDaddy author 4/10/2013

Tried doing this on new PHPR project (version 6.2 build 15275) and it appears that you can now simply add something like this to the "after record added" event to close the page after it is saved:



//Close window after save

echo "

<script type=\"text/javascript\">

alert('Congratulations - your entry was saved');

window.close();

</script>";


Much simpler. I'm not sure why this wasn't working in earlier versions. Bottom line is that now you can forget everything I mentioned in this previous post if using a newer version of PHPR. No need to add custom button or javascript :-)

F
FunkDaddy author 7/29/2014

Slight improvement (works from PHPR 5.3 and UP)



//Save popup and refresh window!

echo "

<script type=\"text/javascript\">

window.parent.location.reload(true);

self.close();

</script>";