Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save file & close window?

Status
Not open for further replies.

Cheech

Technical User
Nov 6, 2000
2,933
EU
Hi
I have the following code that I post a form to with 2 submit buttons. One has the value Save and the other Print.
If the user selects the Print button the page correctly loads, the print dialogue opens & after printing the page correctly closes. However if the user selects Save, even though the correct Open/Save dialogue is triggered, the page is never fully loaded and therefore does not close.
Anyone know how this could be achieved?
Code:
<?php
	if ($_POST["submit"] == "Save") {
		header("Content-type: application/rtf");
		header("Content-Disposition: attachment; filename=MFL_free_text.rtf");
		header("Pragma: no-cache");
		header("Expires: 0");
	 } 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html>
<head>
<title>Self-Evaluation</title>
<style>
body { font-family:Arial, sans-serif; font-size:10pt;}
</style>
</head>

<body onload="self.close();">
<p><a href="javascript:self.close();">Close window</a></p>
<p><strong><?php echo $_POST["client"]; ?></strong></p>
<p><strong><?php echo $_POST["title"]; ?></strong></p>
<p><?php	echo $_POST["free_text_area"]; ?></p>
<p><strong><?php echo $_POST["footer"]; ?></strong></p>
<? if ($_POST["submit"] == "Print") { ?>
<script>
self.print();
</script>
<? } ?>
</body>
</html>

Cheers

[Peace][Pipe]
 
you should go ask in the javascript forum. php does not interact with the browser or the operating system in this way.
 
Just a lucky guess: does it work in firefox but not in IE?

Internet Explorer has a lot of troubles with any standard, and It could very well be its buggy handling of the pragma directive that is the problem here.

Does it work if you remove the last two headers (Pragma and Expires)?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi

DonQuichote said:
Just a lucky guess: does it work in firefox but not in IE?
If the received document comes with header [tt]Content-Disposition: attachment[/tt] is offered for saving.
If the document is saved, is not displayed.
If the document is not displayed, it is not rendered.
If the document is not rendered, embedded scripts are not interpreted.
If script is not interpreted, no [tt]print()[/tt], [tt]close()[/tt], whatever will happen.
If nothing happens, I would say that is the same across browsers.


Feherke.
 
ONLY IF those browsers well behave, and IE is known not to. But that does not answer my question of whether firefox does.

IE takes the Pragma: no-cache header as "never save". This is really frustrating if, for instance, you offer a dynamic PDF file. IE then refuses to save the file and offers the non-existing file to the PDF plug-in, which gives an error.

So this may very well be the issue here, but that remains speculation until it is tested.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi thanks for the feedback/comments.

Your correct, Firefox closes the window correctly. In fact it doesn't even appear to open to the user. IE still doesn't close.
Removing the Pragma & Expires headers has no effect on either browser.

Cheech

[Peace][Pipe]
 
Hi

Cheech said:
Your correct, Firefox closes the window correctly. In fact it doesn't even appear to open to the user. IE still doesn't close.
I would say, that is wrong diagnosis.

By the way, are you opening this printable/savable file in new browser window/tab ?

Feherke.
 
I don't know what you mean by wrong diagnosis? That is what I experience? In Firefox the window opens to print then closes itself. If the user selects save then all that happens is the save dialogue opens.
In IE the print works as above, but the save opens a new window, then the save dialogue opens after the users selects save/open the php window stays open.
Yes its a new window, I am submitting a form using:
Code:
<form action="../print_save.php" method=post name="free_text_form" target="_blank" id="free_text_form">
If you can think of a way to just do the save and have the window close I can easily have 2 files. As long as it is cross-browser.

Cheech

[Peace][Pipe]
 
Hi

Cheech said:
In Firefox the window opens to print then closes itself.
Yes, because the document is rendered and the script is executed. So [tt]self.close()[/tt] does its job.
Cheech said:
If the user selects save then all that happens is the save dialogue opens.
No. A new window or tab opens ( depending on your settings ), the document loads, but while no rendering happens, so the new window/tab would remain empty, FireFox closes it.
Cheech said:
In IE the print works as above, but the save opens a new window, then the save dialogue opens after the users selects save/open the php window stays open.
Yes, because there is nothing that could close it. That is the normal behavior. That is what older FireFox version did. And that is what other browsers like Opera and Konqueror also do.

There was an extension for older FireFox that did such window/tab closing if they was opened but remained empty because the content was saved, not rendered. Seems that feature is now implemented in the browser itself.

Feherke.
 
Thanks for the lesson in browser behaviour(no sarcasm intended), but can it be achieved?

[Peace][Pipe]
 
Hi

Wit abit different approach : use [tt]target="_blank"[/tt] only for the print one.
Code:
<form action="../print_save.php" method="post" target="_blank">
<input type="submit" name="submit" value="Print">
</form>

<form action="../print_save.php" method="post">
<input type="submit" name="submit" value="Save">
</form>

Feherke.
 
This is a "hack" (if you like to call it) using javascript support.

[0] But before scripting it, there is a need in changing the submit button. According to the posted content, your client side submit button is named "submit". This should be avoid, in particular if you need somewhere to use form's submit method. This latter is indeed used in the solution. So I would say change the name to anything oher than "submit".
[tt]
<input type="submit" value="Save" name="submit[red]_x[/red]" /><br />
<input type="submit" value="Print" name="submit[red]_x[/red]" /><br />
[/tt]
The suffix (_x) is just figurative, anything admissible you like.

[0.1] The response page should be changed accordingly, controlling:
[tt] $_POST["submit[red]_x[/red]"][/tt]

[1] The solution proposed is to make an onclick handling to the submit button of value "Save". Like this.
[tt]
<input type="submit" name="submit_x" value="Save" [blue]onclick="setTimeout('document.forms[\''+this.form.name+'\'].submit()',100)"[/blue] /><br />[/tt]

[1.1] In the call to submit() method at some nominal delay (not exactly important in absolute figure, it can be zero), the submit button's value is no longer submitted and the response page will not print nor set the download headers, and the page will be going to close as the onload handler is scripted, if done properly.
 
Hi

tsuji said:
the response page will not print nor set the download headers, and the page will be going to close as the onload handler is scripted, if done properly.
Ok, but how the visitor will save it if
[ul]
[li]"nor set the download headers" -> the Save dialog not pops up automatically[/li]
[li]"the page will be going to close" -> the page stays open only while is loading[/li]
[/ul]

Feherke.
 
Hi

If we involve JavaScript, we can modify my suggestion made at 18 Feb 09 9:33 to use only one [tt]form[/tt] :
Code:
<form action="../print_save.php" method="post" target="_blank">
<input type="submit" name="submit" value="Print" onclick="this.form.target='_blank'">
<input type="submit" name="submit" value="Save" onclick="this.form.target=''">
</form>


Feherke.
 
My understanding is that there is at all time one form, two submit buttons. If not, I have no comment. My proposed solution involves no change on the php file submitted to.
 
Hi

tsuji said:
It's done at the "first" submit.
Now I am totally lost. I do not understand it neither after trying it out.

Just one thing is sure : the JavaScript trick to avoid sending the [tt]submit[/tt]'s [tt]value[/tt] does not work in FireFox.

If I simulate what you intended ( not sending the [tt]submit[/tt]'s [tt]value[/tt] ) I reach to what I objected ( not being able to save the document ).
tsuji said:
My understanding is that there is at all time one form, two submit buttons. If not, I have no comment. My proposed solution involves no change on the php file submitted to.
Yes. This is the context in which I try to understand your suggestion and this is how I tried to implement it.

Feherke.
 
I made no assumption on the target attribute. I assume it is not there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top