If I understand your question clearly. Here is a simple example:
EDIT 1 : What I did is hide the elements that are not to be printed through CSS media.
1.php
<html>
<script>
function sample() {
var x = document.getElementById('txt').value;
window.open('2.php?x='+x);
}
</script>
<input type = "text" id = "txt" />
<button onclick = "sample();"> Sample </button>
</html>
2.php
<html>
<style type="text/css">
@media print
{
#not-print {display:none;}
}
</style>
<body onload = "window.print()">
<div id = 'print'>
<?php
if(isset($_GET['x'])) {
echo $_GET['x'];
} else {
echo 'Hello World';
}
?>
</div>
<div id = 'not-print'>
This is not printed
<div>
</body>
<html/>
EDIT 2 : See code below:
2.php
<html>
<style type="text/css">
@media print
{
#not-print {display:none;}
}
</style>
<body>
<div id = 'print'>
This should be printed!
</div>
<div id = 'not-print'>
This is not printed
<div>
<button onclick = "printdiv()">Print Div </button>
<script>
function printdiv() {
var mywindow = window.open("", '_blank');
mywindow.document.write('<p>' + document.getElementById('print').innerHTML + '</p>');
mywindow.print();
}
</script>
</body>
<html/>