I'm sure this has been asked before but can't seem to find it.

I've a (mostly) php page which has a simple button on it to print. Printing is done with a simple onClick="print.window()". Everything's fine in that it's calling the print.css correctly and what is being printed is what I expect. I'd like to have a method for asking the user if the document printed OK. If they say yes, I'll send them to a page that purges the php session and logs them out. If no, we'll either remain on or redisplay the page they tried to print.

I'm guessing this is going to be a javascript solution, and I have no problem using the jquery framework if that's easier.

Any suggestions would be greatly appreciated.

Thanks in advance,
-Ray

Recommended Answers

All 8 Replies

How about

<script type="text/javascript">
function printAndCheck() {
    print.window();
    var success = confirm("Did it print successfully?");
    if(success)
        window.location.href='logout.php';
    else
        window.location.reload();
}
</script>

and change your onclick to onclick="printAndCheck();"
Haven't tested but I think it should work.

yah EvolutionFallen seems right in addition just be sure the your logout function is int logout.php if youve got to follow his code =).

I'll try that this morning. Thanks to you both for the input. I'm pretty decent at PHP/MySQL, but not so much with javascript, though I'm learning. :)

I'll follow up here with success/fail, but it sure looks like it's the thing that I need.

Thanks!
-Ray

Thanks to EvolutionFallen and code739, it's working. One change, just for purposes of archives, is that the code should read window.print(), not print.window() (of course, I had that wrong in the title of my post, too!) Other than that, it does exactly what I'd hoped for.

Corrected code:

    function printAndCheck() {
    window.print();
    var success = confirm("Did the page print OK?");
    if(success)
    window.location.href='logout.php';
    else
    window.location.reload();
    }
    </script>

Thanks so much!

While the answer provided works as shown, I was encouraged to look at a jQuery/jQueryUI solution which would allow me to use prompts other than the default 'OK' and 'Cancel' which are part of 'confirm()'

That being said, I'm now stumped and hope someone here can point me in the right direction.

I'm linking to jQuery and jQueryUI in the head section of the document and have this in the body:

<script type="text/javascript">
  $(function confirmIt() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:140,
      modal: true,
      buttons: {
        "Yes": function() {
          window.location.href='destroy';
        },
        "No": function() {
          window.print()
          }
      }
    });
  });
  </script>

Then, a bit lower down, I've got this in html:

<div id="custinfo">

    <input class="btn" type="button" onClick="confirmIt()" value="Print This Page" />

    <div id="dialog-confirm" title="Print Confirmation">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Did the page print ok?</p>
    </div><!--dialog-confirm-->

</div><!--custinfo-->

What's happening when I get to the page is that the modal dialog is presented immediately. I'd like that to happen after the user clicks the 'print' button. The buttons within the modal dialog are doing what I'd like. It's just a matter of getting the 'print' button to call the window.print() function, then load the modal.

Any thoughts?

Thanks in adavance.
-Ray

Solved. Some syntactical difference with jQuery took some getting used to and while this is still a bit of a hack, it's working :)

Here was the solution:

    <script type="text/javascript">
      function confirmIt() {
      window.print()
        $( "#dialog-confirm" ).dialog({
          resizable: false,
          height:140,
          modal: true,
          buttons: {
            "Yes": function() {
              window.location.href='somepage.php';
            },
            "No": function() {
              window.print()
              }
          }
        });
      };
      </script>

And in the html:

<div id="dialog-confirm" style="display:none;" title="Print Confirmation">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
    Did the page print ok?</p>
</div><!--dialog-confirm-->

One of the tricks was to set #dialog-confirm to display:none;, or the text within the <p> (after <span>) would display. This is now working, thanks to a LOT of help

Anyway...solved and solution provided for the archives.

-Ray

Glad you got it! I was trying to come up with a jQuery-less way to do ;)

Thanks, EvolutionFallen. I was looking for a non-jQuery method as well, but time constraints got the better of me. I truly appreciate your help!

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.