--Hi Daniweb.
1st of all I apologize if I'm asking this Qn. in a wrong place,but as I came across this subject while I'm doying PhP code and so I thought I'd share it here as I hope someone may be came across the same scenario as I have.
The thing is this:
I want to print a Docement from the contents of a Div which I gave an id=Print_Document here below;

<div id="Print_Document">
<!-- My well formatted Docement Interface contents -->
</div>

And I have a button which triggers My javascript 'Print_Docement' function;
<button onClick="Print_Doc('Print_Document')"><i class="printer"></i></button>
And the Javascript function for printing My document is at the top of My Div holding the Contents to be Printed.Here it is;

function Print_Doc(div)
    {
    var restorepage = document.body.innerHTML;
    var printDiv = document.getElementById(div).innerHTML;

    document.body.innerHTML = printDiv;
    window.print();
    document.body.innerHTML = restorepage;

    }
</script>

Now all is well setup and I can print My Docement fine.
But One thing is for every printed Docement I have to Store its Id in a DB and so I though I'll just create a function to acomplish this task and call it within the Javascript code provided there above and so I did,But I came to realize that the Function to Store Docements Values in DB executes even if the Button to trigger Print_Doc(div) is not yet clicked.
One last thing is even if it is clicked I neede to know how to execute the Function to Store Values when the Ok button of the Print Dialog is Confirmed? Because Someone may go for Print Button and Cancel the Printing exercise right?
I tried lots of Google and I was so frustrated Even this didnt seem to help Me.Any help please?...

Recommended Answers

All 10 Replies

There's no way to access a return value from window.print() I'm afraid.

However, IE5+ and Mozilla 6+ both have the window.onafterprint method, which would do exactly what you require (calls a function after printing has taken place). This is achieved in the following way:

function myFunction(){
    ...
}

window.onafterprint = myFunction

It is very important to note, Chrome & Opera (WebKit) and many mobile browsers do not support this method yet - so cross-browser compatability is not going to happen.

--Thank You mattster,from the Link I posted told Me so that there is No return value from 'window.print()'.Thats why I neede some views as I said I hope some one may heve the same scenario as Mine and I need to know what ways didi they take to get through.
And the suggestion You provided there is highly appriciated coz I never knew of it.And thanks for warning or note there. And so I guess I'm still in a trap.
Lets wait for couple of views right?...

--Now its time to give up,all this time I'v been trying to do some tricks for this matter but seems to stuck with the same thing it keeps record even before confirm button of Print dialog to be clicked.
I hope I'll keep waiting and May be I'll send a suggestin to some Browser experts.
Thanks all but if anything is found please I;ll be glad to know it.

You remind me how we doing it before AJAX. We were loading an iframe that supposed to be js but was originated through Java or PHP and called functions of the main app.js . Now we don't have need for such approach. It seems that what you really want is to update a db when someone click the “print” button (you could do authorization here with the session id). You should read and understand Ajax calls. Do it plain to get it and then use jQuery to do it the easy way

--Cheers jkon but I 'll be happy if You help Me if You have alredy solved such a scenario coz about the Only problem I have is how to know which button the user has clicked on the Print Dialogbox b'coz all the db update based on session id is alredy done and all the stuffs but when user clicks Print button and get the preview on the browser there is a dialod appearing with two buttons Ok and Cancel I'm sure You have alredy seen the Screen shot I posted.Now what heppens is even if the user cancels the printing process the update function of the db is executed so if You got My point and it related to what You have solved then I'll be happy to share the codes I have up to now.

Ajax call when the OK button is clicked and of course you store in session all the needed data for the registered user.

--Ok,May be Im too naive with the Ajax thing b'coz I dont know hw to get with those call Your sugesting.With the aid of a firend and passing through the link that diafol posted and mattster here is what I heve:
1.ON THE HTML INTERFACE WITH THE PRINT BUTTON I HAVE:

<script>
$(document).ready(function(){

    window.onbeforeprint = function() 
    {
    console.log('This will be called before the user prints.');
    };

   window.onafterprint = function() 
   {
    console.log('This will be called after the user prints');

    var Var_BF_Index = getParameterByName('BF_Index');
    var Var_Preview = getParameterByName('Preview');

    $.ajax({
         url:"http://127.0.0.1/prime/Page_Classes/class_do_printing.php",
         data:{
             BF_Index:Var_BF_Index,
             Preview:Var_Preview 
                },
         dataType:"text",
         type: 'GET',
         success:function(data)
         {
          console.log(data)
         }
            })
    };
});


function Print_Doc(div)
    {
    var restorepage = document.body.innerHTML;
    var printDiv = document.getElementById(div).innerHTML;
    var Print_CSS = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #111;' +
        'border-collapse: collapse;'+
        '}' +
        '</style>';

    Print_CSS+=printDiv;
    document.body.innerHTML = Print_CSS; 
    window.print();
    document.body.innerHTML = restorepage;
    }

    function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

AND THE BUTTON FOR INVOKING THE ABOVE FUNCTIO IS:

<button style="margin-left:45px" onClick="Print_Doc('Print_Document')"><i class="printer"></i></button>

AND THE METHOD BEING EXECUTED HERE IN THIS FILE REFFERENCE:
http://127.0.0.1/prime/Page_Classes/class_do_printing.php
IS:

<?php
session_start();

include("class_connection.php");

function JQuery_Printing($Page,$Section,$BF_Index,$Preview)
    {
    $Connection=new System_Users();
    $Connection->Warning_Supressor();
    $MySqli=$Connection->Database_Connect();

    if($BF_Index!="" && $Preview!="")
        {
        $Date_Printed=date("Y-m-d");
        $Time_Printed=date("H:i:s");

        $Sql="INSERT INTO do_printed(Date_Printed,Time_Printed,BF_Index,Print_Name,idDrive)VALUES(?,?,?,?,?)";  
        $Query=$MySqli->prepare($Sql);
        $Query->bind_param("ssisi",$Date_Printed,$Time_Printed,$BF_Index,$Preview,$_SESSION["idDrive"]);
        $Query->execute();
        $dbRows_Affected=$Query->affected_rows;

        if($dbRows_Affected>0)
            {
            echo "<meta http-equiv=Refresh content=1;url='?Page=".$Page."&Section=".$Section."&BF_Index=".$BF_Index."&Preview=".$Preview."'>";
            $Connection->Error_Message="<div class='input-message success'>".$Preview." Document Printed Successful</div>";
            }
        elseif($dbRows_Affected==0)
            {
            $Connection->Error_Message="<div class='input-message info'>No changes were made for this House Bill Document.</div>";
            }
        elseif($dbRows_Affected==-1)
            {
            $Connection->Error_Message="<div class='input-message error'>There seems to be some problems issuing Payments for this House Bill Document,Please try again and if the Problem Persist Contact Administrators.</div>";
            }
        }
    }

    JQuery_Printing($_GET["Page"],$_GET["Section"],$_GET["BF_Index"],$_GET["Preview"]);
?>

So any suggestion from there jkon?

What are you trying to do ? Why you are using js functions not supported ? The main thing is to know who pressed the button ?

--I dont deny anything I'm not good with js,jQuery or Ajax I'm only learning PhP in details right now so whatever is wrong up there You should know that I realy need help and that is far I have go with lots of help not on My own though.

What are you trying to do?

I want when a document is printed by any User say a document with id=10 was printed by Me and You jKon want to Re-Print it You shouldnt get that access until the Administrator resets it so that You can print it again thats all I want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.