0

I have page with a print button. From this page I will print a reportpage by an AJAX call. I've made a reportpage and with jquery the page will be printed when I call that page directly in the browser. In this page I used:

$(document).ready(function () {
   window.print();
});

So this works! But when I call the printpage with an AJAX call from the other page the print command won't fire:

$.ajax({
    url:'pages/reportPage.php',
    method:'POST',
    data:{
        id:id
    },
    error: function() {
        alert('False!');
    },
    success:function(data){
      //alert(data);
        window.location.href = "index.php";
    }
  });

When I display the data (alert(data)), I see the code of the reportpage, so the reportpage is successfull called.

Does anybody knows why this won't work with an AJAX call?

Nico van Wijk
  • 241
  • 1
  • 9
  • Printing is a client-side activity and you're calling a PHP script on the server-side that does not render on the client. – Jay Blanchard Aug 13 '20 at 12:30
  • You're successfully making the AJAX call, but you don't do anything with the result. Instead you just redirect the user to `index.php`. If you want the browser to print the page then you at least need to render that page in the browser. – David Aug 13 '20 at 12:30
  • Have you tried these solutions? https://stackoverflow.com/questions/23707251/how-do-i-print-different-page-using-javascript-jquery-ajax https://stackoverflow.com/questions/8240472/printing-a-web-page-using-just-url-and-without-opening-new-window – Frequent Flyer Italia Aug 13 '20 at 13:37
  • Please refer to https://stackoverflow.com/questions/2365220/jquery-document-ready-not-firing-after-window-location-href and try window.location instead. – mkane Aug 13 '20 at 14:36

0 Answers0