i have the following function and I can't get the javascript print window to work. it keeps returning a javascript error.

function Print_Info()
{
$printquery = "SELECT prints FROM coupons WHERE coup_id = '$coup'";
$printresult = mysql_query($printquery);
$prints = mysql_result($printresult,0, 0);
$prints = $prints + 1;

	$addprintquery = "UPDATE coupons SET prints = '$prints' WHERE coup_id = '$coup'";
	$addprintresult = mysql_query($addprintquery);

	echo "<SCRIPT language=javascript>window.print();</SCRIPT>";

}

anyone have any ideas? thx

    Replace the echo, with this.

    
    print "<script>";
    print "window.print();";
    print "</script>";
    
    

    But since all that is in a function, you will have to call that function somewhere in that page.
    Tell me if that works.

    -Blake

      originally posted by jeff_borden it keeps returning a javascript error

      Dare I ask you to post the issue, then, to a Javascript forum? 😉 JS is so much harder to debug, as its error messages tend to be quite a bit more cryptic than PHP's.....

      What's the JS error? And what really is this PHP function doing? When you tell the browser to do this Javascript operation, you are telling it to open the browser/OS's print dialog, assuming of course that it has one. Is that really what needs done at this point?

      Let me tell you why I ask. This PHP function appears to do some logic, some variable assignment, etc. However, since there is no return statement, the only thing I see it doing in practice is outputting HTML/JS to the browser...

      I daresay there are more issues here to deal with...of course, I have been wrong before....

        As dalescosp mentioned, javascript is harder to debug, especially for a php forum. 😃

        Why don't you give this a try though. Put this betwen the head tags in your document

        <script>
        
        function printWindow(){
           bV = parseInt(navigator.appVersion)
           if (bV >= 4) window.print()
        }
        
        
        </script>

        Then make the function a link.

        echo '<a href="javascript: printWindow()">Print This Page</a>';

        Just make sure you don't leave a space between the colon after javascript and printWindow. The forum automatically converts it into a smiley so i had to drop the space in.

        Cgraz

          thx for the replies. basically what i am wanting to do is when the user clix on the print button, call the function that logs that this window has been printed... then print it.

            What you could do then is when they click the link, open up another window that uses php to log that the page is going to be printed. Then use onload="javascript: print.Window" (w/out the space) on that page so it prints automatically whenever it's loaded. You can even have it say "your file is now printing", then have the window close after 5 seconds.

            Cgraz

              This echo statement should work: echo '<script language="JavaScript">window.print();</script>';

                Originally posted by jeff_borden
                basically what i am wanting to do is when the user clix on the print button, call the function that logs that this window has been printed... then print it.

                Like I mentioned above, you can do it this way.

                function Print_Info() 
                { 
                $printquery = "SELECT prints FROM coupons WHERE coup_id = '$coup'"; 
                $printresult = mysql_query($printquery); 
                $prints = mysql_result($printresult,0, 0); 
                $prints = $prints + 1; 
                
                $addprintquery = "UPDATE coupons SET prints = '$prints' WHERE coup_id = '$coup'"; 
                $addprintresult = mysql_query($addprintquery); 
                
                print "<script>";
                print "window.print();";
                print "</script>";
                }
                

                Then for the print button use this:

                <input type="button" name="Button" value="Print this page!" onClick="Print_Info()">

                That should work,
                -Blake

                  Originally posted by batman
                  Then for the print button use this:

                  <input type="button" name="Button" value="Print this page!" onClick="Print_Info()">

                  That should work,
                  -Blake

                  Umm, using a JS "onClick" to call a PHP function? "Holy Code Convolutions, Batman!" 🙂

                    What? You saying that's wrong?

                    -Blake

                      This is how I would do it, and it works I tested, at least the print does.

                      Make the coupon page like normal, then show this where you want the print button to be:

                      <a href="print.php">Print this page!</a>

                      Then the coding for the print.php code will look like this:

                      $printquery = "SELECT prints FROM coupons WHERE coup_id = '$coup'"; 
                      $printresult = mysql_query($printquery); 
                      $prints = mysql_result($printresult,0, 0); 
                      $prints = $prints + 1; 
                      
                      $addprintquery = "UPDATE coupons SET prints = '$prints' WHERE coup_id = '$coup'"; 
                      $addprintresult = mysql_query($addprintquery);
                      
                      print "<script>";
                      print "window.print();";
                      print "</script>";
                      include 'YOUR COUPON PAGE HERE';
                      
                      

                      That should do the trick!

                      -Blake

                        Originally posted by batman
                        What? You saying that's wrong?

                        -Blake [/B][/QUOTE] Well, let me just say I've not seen it done that way before.  Or else I'm confused about what Print_Info() is.  It sure looked like a PHP function---that is, it's in a file on the server side, and its code never gets sent to the browser.  How would Javascript call it? :eek:

                          Yes, I just figured that out. That's why I posted a solution 2 posts up.

                          -Blake

                            Write a Reply...