How to Print Div Contents in Php

QuestionsCategory: PHPHow to Print Div Contents in Php
Ishu asked 2 months ago

When Going to print using CTRL+P ,

It prints the whole body html contents.

But I want to print only a particular div content. Do not print the other content outside the div.

1 Answers
Best Answer
devchandan Staff answered 2 months ago

If You want to print the section or Div contents, then please do the following…
By using the following code you can print the contents in a div or section.
 
 

<!-- For Print the Contents Starts -->

<script language="javascript" type="text/javascript">

function printDivContents(divID) {

//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;

//Get the HTML of whole page
var oldPage = document.body.innerHTML;

//Reset the page's HTML with div's HTML only
document.body.innerHTML = 
"<html><head><title></title></head><body>" + 
divElements + "</body>";

//Print Page
window.print();

//Restore orignal HTML
document.body.innerHTML = oldPage;


}
</script>

<!-- For Print the Contents / Ends-->

 
Please Check.. the solution here.. Print Div Contents
https://www.stackofcodes.in/2018/11/06/print-a-div-contents-in-php/