print content from div on another page

Asked

Viewed 195 times

1

I have a php page that receives values from a form (post) and wanted to print this results on paper.

It turns out that by clicking the button the value in the textarea does not appear in the print.

On the origin page I have this form with textarea and a button to print, so:

<form method="post">
<p><textarea name="textoImprimir" cols="" rows="" class="anamnese-tarea">quero imprimir este texto que acabei de digitar.</textarea></p>
<p><iframe src="imprimir.php" name="frame1" style="display:none;"></iframe>
   <input type="button" onclick="frames['frame1'].print()" value="print!"></p>
</form>

On the page print.php I have so:

<?php
$textoImprimir = $_POST['textoImprimir'];
?>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Imprimir</title>
<style>
html, body { width:100%; height:100%; margin:0px; }
.fundo { background: url(../_imagens/fundo_print.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}
@page {
  size: A4;
  margin: 15mm 15mm 15mm 15mm;
}

table { top:18%; bottom:20%; position:absolute; }
</style>
</head>
<body class="fundo">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="20%" align="center" valign="middle">ALGUM TEXTO</td>
  </tr>
  <tr>
    <td height="80%" align="justify" valign="top"><?php echo $imprimir;?></td>
  </tr>
</table>
</body>
</html>

1 answer

2


Change the print form to be this way, so we will pass via POST, so that we can print in the print file.php

<form method="post" action="imprimir.php" enctype="multipart-form/data">
    <textarea name="textoImprimir" id="textoImprimir" cols="" rows="" class="anamnese-tarea">quero imprimir este texto que acabei de digitar.</textarea>
    <input type="submit" value="Imprimir!">
</form>

Inside the print.php you poe

<script>window.print();</script>

And change

$textoImprimir = $_POST['textoImprimir'];

for

$imprimir = $_POST['textoImprimir'];
  • 1

    that’s right, simplest thing... at the bottom of the page I added the forwarding to the source page (regardless if print, save to pdf or cancel) and it was perfect. Thank you.

  • I came across a problem: if I add css or even an image on the print.php page, it no longer works. There’s a way around this?

  • You added that way Peter?

  • André, on the other page I put a table and in the middle of the cell an image, like this: <table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-bottom:#666 thin Solid;"> <tr> <td align="center" valign="bottom"><img src="image.png" width="100" height="100" /></td> </tr> <tr> <td>&nbsp;</td> </tr> </table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.