I wanted to print only my HTML Table contents within the webpage that I am currently working on, however, when I tried to print it, it says "undefined". Why is that so? and is there any other way I could print my table that is connected in a database? Here are my codes:
<script>
function printDiv(tableview) {
var printContents = document.getElementsByClassName(tableview).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
<div class="printBtn">
<img src="images/printBtn_hover.png" class="printBtn_hover" onclick="printDiv('tableview')">
<img src="images/printBtn.png" class="printBtn" alt="print" onclick="printDiv('tableview')">
</div>
<div class="tableview">
<table class="table">
<tr>
<th class="theader">Entry Code</th>
<th class="theader">User ID Number</th>
<th class="theader">Name</th>
<th class="theader">Date of Visit</th>
<th class="theader">Time of Visit</th>
</tr>
<tbody>
<?php
while ($row = mysqli_fetch_array($searchResult)):?>
<tr id="trow">
<td class="tdata"><?php echo $row['qr_id'];?></td>
<td class="tdata"><?php echo $row['idNum'];?></td>
<td class="tdata"><?php echo $row['lastName'].", ".$row['firstName']?></td>
<td class="tdata"><?php echo $row['date'];?></td>
<td class="tdata"><?php echo $row['time'];?></td>
</tr>
<?php endwhile;
?>
</tbody>
</table>
</div>