简体   繁体   中英

Call Javascript function using PHP

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM