PHP/Javascript: print preview selected page

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP/Javascript: print preview selected page

Post by cjkeane »

hi,

i need to open a print preview page and only display the contents of a defined page. Currently i'm using the following code, but it opens a print preview window of the current page, not the page defined in the href. Does anyone have any ideas on how to resolve this?

Code: Select all

<?php
    echo '<td nowrap><a href="print_preview_invoice.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num'].'" target="_blank" onclick="print();">'.$invoice.'</a>' . " / " . '<a href="print_preview_timesheet.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num'].'" target="_blank" onclick="print();">'.$timesheet.'</a></td>';
?>
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP/Javascript: print preview selected page

Post by Christopher »

Perhaps you could open a new window containing the page you want to print and call the Javascript print() function from within that page.
(#10850)
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/Javascript: print preview selected page

Post by cjkeane »

is it possible to call print() upon opening the page e.g. on load?

i just tried :

Code: Select all

<?php
echo '<td nowrap><a href="print_preview_invoice.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num1'].'"  >'.$invoice.'</a>' . " / " . '<a href="print_preview_timesheet.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num1'].'" >'.$timesheet.'</a></td>';
>?
when print_preview_invoice.php opens, i have <body onload="print()">

This opens the invoice, but also opens the print preview window, so it in fact opens two windows. is there any way to resolve this? i just want/need the print preview window to open.
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: PHP/Javascript: print preview selected page

Post by Tiancris »

Instead of calling print() in body onload, add a button and call print() in onclick event. So you can see the preview and then print only if you want.

[text]<input type="button" onclick="print()" class="hidden" />[/text]

And set this style to avoid printing the button:

[text]<style type="text/css" media="print">
.hidden {display:none}
</style>[/text]
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/Javascript: print preview selected page

Post by cjkeane »

yes i understood. It works perfectly thanks! now the button is hidden when the button is clicked.
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: PHP/Javascript: print preview selected page

Post by Tiancris »

:D Exactly. And the same is applicable to any visible object that you don't want to be printed.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/Javascript: print preview selected page

Post by cjkeane »

yes, i realized that as well! perfect! thanks again.
Post Reply