Print a Dynamic php page without displaying it
Posted: Mon Apr 21, 2008 9:06 am
In this example i will show you how to print a dynamic php page with dynamic contents without showing up the page itself.
This can be useful in situations where you need to print a dynamic page but you should not display it.
I had a scenario like this, where i use a form to gather information, and these information are not saved up in any database, and it is printed in a different pattern with images etc, and i should not display the printing page.
so got up with this mix of js and php code which does it,
hope it might be useful for some..
Line by line explanation of the code
this function diplays div "one" and hides div "two"
this function diplay div "two" and hides div "one" and then prints div "two" and then displays div "one" again
Body tag display div "one" before submit and div "two" after submit, the condition checks it with submit button, js body onload event is used to call the function
the form which takes the input values is in div "one"
the section which displays the dynamic content from the form is in div "two"
This can be useful in situations where you need to print a dynamic page but you should not display it.
I had a scenario like this, where i use a form to gather information, and these information are not saved up in any database, and it is printed in a different pattern with images etc, and i should not display the printing page.
so got up with this mix of js and php code which does it,
hope it might be useful for some..
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script language="javascript">
function beforeSubmit(){
document.getElementById("one").style.display = "block";
document.getElementById("two").style.display = "none";
}
function afterSubmit(){
document.getElementById("one").style.display = "none";
document.getElementById("two").style.display = "block";
window.print();
document.getElementById("one").style.display = "block";
document.getElementById("two").style.display = "none";
}
</script>
<body <?php if(isset($_REQUEST[btn_submit])){ ?> onLoad="javascript: return afterSubmit();" <?php }else{ ?> onLoad="javascript: return beforeSubmit();" <?php } ?>>
<div id="one">
<form name="form1" method="post" action="./test21.php">
<table width="600" border="0" align="left">
<tr>
<td width="300" align="right">name:</td>
<td width="300" align="left"><input type="text" name="str_name" size="50"></td>
</tr>
<tr>
<td width="300" align="right">email:</td>
<td width="300" align="left"><input type="text" name="str_email" size="50"></td>
</tr>
<tr>
<td width="300" align="right" colspan="2"><input type="submit" value="print" name="btn_submit"></td>
</tr>
</table>
</form>
</div>
<div id="two">
<table width="600" border="0" align="left">
<tr>
<td width="300" align="right" colspan="2"><?php echo $_REQUEST[str_name]; ?>' mail id is <?php echo $_REQUEST[str_email]; ?></td>
</tr>
</table>
</div>
</body>
</html>Line by line explanation of the code
this function diplays div "one" and hides div "two"
Code: Select all
function beforeSubmit(){
document.getElementById("one").style.display = "block";
document.getElementById("two").style.display = "none";
}Code: Select all
function afterSubmit(){
document.getElementById("one").style.display = "none";
document.getElementById("two").style.display = "block";
window.print();
document.getElementById("one").style.display = "block";
document.getElementById("two").style.display = "none";
}Body tag display div "one" before submit and div "two" after submit, the condition checks it with submit button, js body onload event is used to call the function
Code: Select all
<body <?php if(isset($_REQUEST[btn_submit])){ ?> onLoad="javascript: return afterSubmit();" <?php }else{ ?> onLoad="javascript: return beforeSubmit();" <?php } ?>>the form which takes the input values is in div "one"
Code: Select all
<div id="one">
<form name="form1" method="post" action="./test21.php">
<table width="600" border="0" align="left">
<tr>
<td width="300" align="right">name:</td>
<td width="300" align="left"><input type="text" name="str_name" size="50"></td>
</tr>
<tr>
<td width="300" align="right">email:</td>
<td width="300" align="left"><input type="text" name="str_email" size="50"></td>
</tr>
<tr>
<td width="300" align="right" colspan="2"><input type="submit" value="print" name="btn_submit"></td>
</tr>
</table>
</form>
</div>the section which displays the dynamic content from the form is in div "two"
Code: Select all
<div id="two">
<table width="600" border="0" align="left">
<tr>
<td width="300" align="right" colspan="2"><?php echo $_REQUEST[str_name]; ?>' mail id is <?php echo $_REQUEST[str_email]; ?></td>
</tr>
</table>
</div>