-1

is it possible to call this function to print a document using PHP?

Here is my code guys..

<html>
<head>
<script language="javascript">
   function Clickheretoprint()
   { 
     var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
         disp_setting+="scrollbars=yes,width=900, height=700, left=100, top=25";  

     var docprint=window.open("","",disp_setting); 
         docprint.document.open(); 
         docprint.document.write('<html><head><title>Testing</title>'); 
         docprint.document.write('</head><body onLoad="self.print()" style="width: 900px; height="auto" font-size:16px; font-family:arial;">');          
         docprint.document.write('<h1>Hello World!</h1>');          
         docprint.document.write('</body></html>'); 
         docprint.document.close(); 
         docprint.focus(); 
     }
</script>
</head>
<body>
</body>
</html>

Thank you.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
Adrian
  • 29
  • 9
  • 2
    Just add a call to the function – j08691 Sep 23 '16 at 16:28
  • 2
    You don't "call JavaScript from PHP", you "include a JavaScript function call in your output". PHP is a server-side language. Javascript is a client-side(browser) language. Browser and server can communicate but can not call a function from one another. – mertyildiran Sep 23 '16 at 16:31
  • It wont work sir.. I have tried changing the function for aler("Hello World"); and adding a php code to echo clickheretoprint() and it works.. i wonder why its not working on this codes.. T_T – Adrian Sep 23 '16 at 16:36
  • Are you sure your Clickheretoprint() method actually works? I'm assuming that it will call it no problem, but if the script it'self doesn't work then... there is your answer – ProEvilz Sep 23 '16 at 16:46
  • @j08691 : I have tried to echo the function name using PHP but it's not working.. just like the answer below.. can you help me make a code for this? – Adrian Sep 23 '16 at 16:47
  • @AshleyBrown : I have tried putting this inside body and it works.. i dont know why it wont work having an echo for it using PHP – Adrian Sep 23 '16 at 16:57

1 Answers1

1

Your javascript function works as written (opens a new window, writes to it, then opens the print dialog), and I've inserted the function call using PHP below:

<script language="javascript">
function Clickheretoprint() {
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
    disp_setting+="scrollbars=yes,width=900, height=700, left=100, top=25";

    var docprint=window.open("","",disp_setting);
    docprint.document.open();
    docprint.document.write('<html><head><title>Testing</title>');
    docprint.document.write('</head><body onLoad="self.print()" style="width: 900px; height="auto" font-size:16px; font-family:arial;">');
    docprint.document.write('<h1>Hello World!</h1>');
    docprint.document.write('</body></html>');
    docprint.document.close();
    docprint.focus();
}
<?php
//calling function using PHP
echo 'Clickheretoprint();';
?>
</script>

I think you're having another issue:

  • Is your browser preventing popups? (If that's the case, the window is never open, therefore docprint.document is undefined)
  • You probably don't need to call the function via PHP - but perhaps using a click event in javascript.

In addition, docprint.document.open(); is unnecessary, considering you already opened the window with var docprint=window.OPEN("","",disp_setting);

lookdad
  • 98
  • 7
  • Yes.. my browser is preventing pop-ups.. (no wonder why..haha).. and yes sir i tried putting inside body.. and it works.. but needing to call it in php.. – Adrian Sep 23 '16 at 17:05
  • 1
    @adrian Why do you need to call it via PHP, and did you use the PHP code I specified? (It does call via the PHP line for me) – lookdad Sep 23 '16 at 17:07
  • 1
    Yes sir.. it does work now.. i have configured my settings to allow pop-ups as enabled.. – Adrian Sep 23 '16 at 17:15