How to add a print button to a web page
To add a print button to a web page using PHP, you can use the following steps:
- Create a button element in your HTML code with a unique id, such as "print-button".
<button id="print-button">Print</button>
- Use JavaScript to attach an event listener to the button element that listens for a click event.
document.getElementById("print-button").addEventListener("click", function() {
window.print();
});
Watch a video course
Learn object oriented PHP
- Optionally, you can also use JavaScript to add a stylesheet to the page that is specifically for printing, and then use CSS media queries to hide elements on the page that you don't want to print.
<link rel="stylesheet" type="text/css" media="print" href="print.css">
@media print {
/* styles to hide elements on the page while printing */
}
Finally, you should test the print button in different web browsers and make sure that it works as expected.