-6

How can I print a specific part of a page using pure PHP?

Note: I am aware that this can be done in javascript, but I am looking for a solution in PHP.

EDIT: I just want to add functionality so that if a user disables javascript, they won't be confused.

  • Why do you insist on using PHP anyway? – Mystical Jan 09 '19 at 00:20
  • @EternalDarkness so that my website will still be functional even if javascript is disabled. – user10887021 Jan 09 '19 at 00:21
  • Hi @user10887021, can you elaborate on what you're trying to do - do you have any sample code of what you've tried? I mean, why wouldn't echo'ing the content work? – Justin Schwimmer Jan 09 '19 at 00:24
  • @JustinSchwimmer I'm just asking for a way to print the page using PHP, **without any javascript.** – user10887021 Jan 09 '19 at 00:27
  • @user10887021 _Just use javascript._ If the user disables javascript, then they should expect to have less functionality when viewing websites. Additionally, I don't think there is a simple way to print a specific part of a page, in pure PHP, at least. – Mystical Jan 09 '19 at 00:31
  • @EternalDarkness what if I want to know how to do it just for the heck of it? I mean surely there must be a way to this in PHP ... I wouldn't mind if it is complex. – user10887021 Jan 09 '19 at 00:33
  • 2
    Your PHP code runs on your server. Your server does not have access to your visitor's printer. Why are you so sure that it's possible? – Greg Schmidt Jan 09 '19 at 00:39
  • @GregSchmidt Fine, how about this, how can I add print functionality that works even if the user disables javascript? – user10887021 Jan 09 '19 at 00:41
  • Add some text that tells them how to use the Print function of their browser? Let's put it this way. In order to print the page, code needs to run on their computer. JavaScript is how you run code on their computer. – Greg Schmidt Jan 09 '19 at 00:45
  • ctrl-P in most browsers –  Jan 09 '19 at 00:46
  • @GregSchmidt can I not trigger ctrl+p manually, using php? – user10887021 Jan 09 '19 at 00:48
  • 3
    No! That's something that has to happen on the visitor's computer, and PHP runs on your server. You're asking the equivalent of "how can I start my car remotely, but without using a remote car starter". – Greg Schmidt Jan 09 '19 at 00:50

1 Answers1

0

Use javascript's window.print() in normal cases where javascript is enabled, and simply add a <noscript> tag to tell the user that they have to enable javascript in order to print.

Something like this:

<noscript>Please enable javascript in order to print the page.</noscript>
Mystical
  • 2,505
  • 2
  • 24
  • 43