Closing window

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here is a problem I encounter often but haven't solved.

In a form I have a submit button. When clicked, I want to do some processing  
so I check with the isset($_POST['the_button_name']).  (I keep all my code  
above the html code).  Now, once I have done that processing, I want the  
window to disappear.  Is it as simple as adding an on_click event to close  
the window?  Will the processing then still take place?


Re: Closing window

Quoted text here. Click to load it

If the window was not opened with javascript, you might get a warning if you  
try to close it.

The best (only?) way to close it in this situation is to spit out some  
javascript after you process the $_POST:

<script type="text/javascript">

and then exit your script.  Your window will then close after the processing  
has been done.

Quoted text here. Click to load it


Re: Closing window

Quoted text here. Click to load it

First I tried with simply putting in an onClick.  That killed the window,  
but didn't do the processing. I then changed the code as you suggested (or  
as I think you suggested) and nothing showed up in the window.

If I modified it further and put your suggestion in the <head> section, then  
the window doesn't even open.
If I enapsulate it in a function inside the scritpt (in the <head>) and  
blindly call that function, I get a stack overflow.

Here is how I modified itas per your post (as I understood it)

if (isset($_POST['editText'])) {
    // write this little file to check that the processing is done
    $handle = fopen("junk.txt", 'w');
    fwrite($handle,"The writing succeeded");

  // I put it here
   <script type="text/javascript">
   (html leading up to the table stuff)
<table width="600" border="1">
  <tr align="center">
   <input name="editText" type="button" value="Submit Changes">
     (ending html stuff)  

Re: Closing window

Try this instead:

 if (isset($_POST['editText'])) {
    // write this little file to check that the processing is done
    $handle = fopen("junk.txt", 'w');
    fwrite($handle,"The writing succeeded");

  // ### Try this instead:
   echo "<script type=\"text/javascript\">".

Re: Closing window

Quoted text here. Click to load it

I copied this in and tried it.  It worked like a charm.  Thanks a lot.  Now  
for another one.

I open this page to edit the text area.  When it is submitted, a session  
variable gets set to the contents and now the window disappears.  I have a  
text field before the link to open this edit page.  It is filled with the  
first, say, 45 characters of this text field.  Is there a way to have this  
field updated when the editing page disappears and focus returns to the  
original page?   (This falls under the "it would be very nice" category).  
Currently I have a "refresh button"/

Thanks again.  This technique will be very useful for me now and in the  


Re: Closing window


I think it could be a bit too tricky for you, especially after you said
that you "copied this in and tried it." :-\

Let me get this straight
- you have a page with a textarea and a link
- when you click the link, a window is opened
- you do some processing with the poped-up window
- the poped-up window is closed after processing
- you want the original window to show the changes you did in the popup

The bad news is, I'm not really prepared to go into details on this as
it could get tricky for you. Other here who have the time are welcome
to collaborate further.

However, the good news is that I'll leave some clues for you to

First of all, you need to use javascript to open the popup window as a
dialog using javascript function window.showModalDialog (do a research
on google about this javascript function).

Instead of:
<a href="popup_window.php" target="_blank">click me</a>
you'd use:
<a href="JavaScript: my_function_to_open_window()">click me</a>

Where in my_function_to_open_window() it will be something like:

function my_function_to_open_window()
   // open window using window.showModalDialog()

   // do stuff to refresh the page, e.g.: document.forms[0].submit()
   // which will refresh the page

So, in summary, you do need Javascript to do what you want to do, I
can't think of any other way to handle two windows without javascript
(ofcourse I'm open to ideas).

Kind Regards,


Re: Closing window

An onclick didn't work.  I tried your suggestion of the
<script type="text/javascript">

but where do you place it?  If I put it in the html area (in the <head>)  
then the window disappears immediately.  If I put it in the <?php ?> area at  
the top in the the check for the button being clicked, then the window  
appears empty.

Here is my code:  (cut down a bit).  Where do I put the script?

if (isset($_POST['editText'])) {
     //test file to check on success for when window disappearson its own
     $handle = fopen("junk.txt", 'w');
     fwrite($handle, "Success");

(html area form/table)
   <textarea name="txtShort" cols="80" rows="20"><?php
            echo $_SESSION['shortDesc'];?>
  <input name="editText" type="submit" value="Submit Changes">
(finishing html stuff)

Re: Closing window

Hash: SHA1

Sheldon Glickler wrote:

Quoted text here. Click to load it

What window? Think about elinks and wget users.

 "Do not try and bend the window. That's impossible. Instead ... only try to
realize the truth."
 "What truth?"
 "There is no window."

Now, seriously: PHP is intended to output a stream of bytes (typically HTML)
to somewhere (typically a web browser). It is not intended to mess with the
user interface on the client side.

- --  
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

El 67% de las estadísticas son falsas
Version: GnuPG v1.4.2 (GNU/Linux)


Re: Closing window

Document processing the form:

Here you proccess the $_POST information.
After processing such information: header ( 'Location:  
cierraVentana.html' );

Code for cierraVentana.html: <script>window.close ();</script>

Might this be useful for you?


Quoted text here. Click to load it

Site Timeline