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 Chris Miller 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]
 
Hi

tsuji said:
I made no assumption on the target attribute. I assume it is not there.
Without the [tt]target="_blank"[/tt] the current content will be replaced by ../print_save.php. As I understand, that is not what Cheech wants.

Even worst, if the document containing the Save/Print [tt]form[/tt] was loaded into window/tab opened with JavaScript, it will be replaced then closed.
Feherke said:
Now I am totally lost.
My mistake, I wrote it too soon. Now I am totally lost.

Feherke.
 
Hi

tsuji said:
How about just do it to see?
Unless explicitly stated otherwise, I always try the code I post or I post about.

And given your impressing number of JavaScript stars, I would never comment on you solution without trying twice.

Feherke.
 
Now that I can sit down to answer with some demo.

[3] I am based on the op's observation:
>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.

[3.1] I think that is a rather faithful description for some situation; but it certainly may not be a universal happening - and that caprice does give concern and not the concern here. I think I would give credibility of that happening. If for some cases that doesn't happen, the whole debat is off the mark.

[3.2] To give reliable reproducibility, I can add exit to the op's php-html script. (I add readfile() just for more fun.) Suppose it is called "z.php"
[tt]
<?php
//z.php
if ($_POST["submit[red]_x[/red]"] == "Save") {
header("Content-type: application/rtf");
header("Content-Disposition: attachment; filename=MFL_free_text.rtf");
header("Pragma: no-cache");
header("Expires: 0");
[blue]readfile("MFL_free_text.rtf"; //or other real name of the file on the server
exit;[/blue]
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[ignore][/ignore]">
<html>
<head>
<title>Self-Evaluation</title>
<style>
body { font-family:Arial, sans-serif; font-size:10pt;}
</style>
</head>

<body onload="[blue]window.opener=self;[/blue]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[red]_x[/red]"] == "Print") { ?>
<script>
self.print();
</script>
<? } ?>
</body>
</html>
[/tt]
[3.3] In case the loading window is not opened by some other script, .close() may be blocked, in particular on mozilla. In that case, the onload handler can be elaborated with calling to PrivilegeManager for user to allow UniversalBrowserWrite privilege. But, let's keep it simple with the chain of callings as described in [4] below.

[4] Let make the window to be closed is spawn to existence via script, say, through window.open.

[4.1] Let the parent of all those happenings be triggered by the page "x.htm".
[tt]
<html>
<head>
<title>x.htm</title>
<script language="javascript">
function doit() {
window.open ("[blue]y.htm[/blue]");
}
window.onload=doit;
</script>
</head>
<body>
<div>testing the print-save htm on script opened window.</div>
</body>
</html>
[/tt]
[4.2] The print-save form page is "y.htm" as called by "x.htm" of [4.1].
[tt]
<html>
<head>
<title>y.htm</title>
</head>
<body>
<form name="formname" method="post" action="[blue]z.php[/blue]">
<input type="text" name="client" value="clientname" /><br />
<input type="text" name="title" value="clienttitle" /><br />
<textarea name="free_text_area">text area input</textarea><br />
<input type="text" name="footer" value="footervalue" /><br />
<input type="submit" name="submit_x" value="Save" [blue]onclick="setTimeout('document.forms[\''+this.form.name+'\'].submit()',5000);"[/blue] /><br />
<input type="submit" name="submit_x" value="Print" /><br />
</form>
</body>
</html>
[/tt]
[4.3]
[4.3.1] Note that it is submitted to z.php as shown in [3.2].
[4.3.2] submit_x name is implemented avoiding "submit" as name---it is always considered bad by me.
[4.3.3] In the setTimeout(), it does need some more time for a fast traffic. (My original post may not be correct. Time delay's length does matter, a compromised and reasonable ad hoc time delay is needed. I make it 5 sec.)

[5] Now, the chain of action is:
[tt] x.htm ---> y.htm (new window) ---> z.php[/tt]
That's about it the set up.

ps: This post is edited real-time, so watch the typos and all.
 
Hi

tsuji said:
I can add exit to the op's php-html script.
This changes too much. Sorry, I have no time to follow your ideas.

However some quick last notes.
tsuji said:
[tt]window.opener=self[/tt]
This will not work in FireFox. The [tt]opener[/tt] property although is not read-only, ignores new values.
tsuji said:
[tt]<input type="submit" name="submit_x" value="Save" onclick="setTimeout('document.forms[\''+this.form.name+'\'].submit()',5000);" />[/tt]
No idea if this is intended or not, but in FireFox the [tt]form[/tt] will be submitted twice :
[ol]
[li]By the click, with [tt]submit_x[/tt] "Save" -> the savable document will be returned[/li]
[li]By the [tt]setTimeout()[/tt], without [tt]submit_x[/tt] [tt]value[/tt] -> the printable document without [tt]self.print()[/tt] will be returned[/li]
[/ol]
So the visitor has to press Ok in the save's first dialog in less the 5000 milliseconds, otherwise the document will be replaced with the one which closes the window.


Feherke.
 
If ff ignores that, it goes on. I find it no problem to continue the next statement. I can take it out no problem neither.

In any case, the op disappeaed from sceen. I have said what I have to say, thanks for your attention.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top