I'm trying to call a javascript function with PHP.
Javascript function as below:
<script type="text/javascript">
function printDiv(divName)
{
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
I use this code to run function with PHP:
if (array_key_exists('ykpost', $_POST)) {
// do something with php
$printyk = '<script type="text/javascript">printDiv("PrintableArea");</script>';
echo $printyk;
}
Print content as below:
<div id="printableArea" class="printDiv">
//some content with created by php
</div>
I post with this form:
<form id="ykbarcodepost" method="post">
<input type="submit" class="btn" value="Print Barcode" name="ykpost" />
</form>
But it doesn't open print window. If I change $printyk
variable as "Hello" it writes Hello.
So thought problem with javascript. But when I try with another button as below
<input type="button" class="btn" onclick="printDiv('printableArea')" value="Print Barcode" />
It shows printDiv. Is there anything which I couldn't see ?