简体   繁体   English

单击按钮时打印页面不起作用

[英]Print page not working when button click

I created a PHP page to print invoice of my project. 我创建了一个PHP页面来打印我的项目的发票。 I need to print this page automatically after saving my invoice. 保存发票后,我需要自动打印此页面。 I used jquery $.post method to open print.php page, 我使用jquery $ .post方法打开print.php页面,

Save invoice button 保存发票按钮

$.post("print.php",{
    desc: $desc,
    }, function(data,success){
    alert(success);
});

and i used javascript window.onload = function(){window.print();} in print.php page. 我在print.php页面中使用了javascript window.onload = function(){window.print();} but its not working, nothing printing. 但它不起作用,什么也没打印。 Then i open print.php page in browser and it was printed as i want.I don't need to redirect to print.php page. 然后我在浏览器中打开print.php页面,它是按我的意愿打印的。我不需要重定向到print.php页面。 Is there anything wrong in my code. 我的代码有什么问题吗? How to print this page ? 如何打印此页面?

Edit 编辑

I don't need to save or get result from this $.post . 我不需要保存或从$.post获取结果。 I just need to print invoice with desc data. 我只需要打印带有desc数据的发票。

Problem:The page is not loading so window.onload will not work.This is because you have posted with ajax. 问题:该页面未加载,因此window.onload将不起作用,这是因为您使用ajax发布了。 Solution:Try posting the invoice details by using a form. 解决方案:尝试使用表单过帐发票详细信息。

Try chaining a done() after the post() . 尝试在post()之后链接一个done() post()

$.post( "print.php", { desc: invoiceData })
   .done(function( data ) {
       alert( "Success: " + data );
});

Heads up! 小心!

Alert the results from requesting print.php using done() instead of success() because: The jqXHR.success() , jqXHR.error() , and jqXHR.complete() callback methods are removed as of jQuery 3.0. 从通知要求的结果print.php使用done()的,而不是success()因为jqXHR.success() jqXHR.error()jqXHR.complete()回调方法也会被删除的jQuery 3.0。 You can use jqXHR.done() , jqXHR.fail() , and jqXHR.always() instead. 您可以使用jqXHR.done()jqXHR.fail()jqXHR.always()代替。

If you just want to print the response of ajax then use below code. 如果您只想打印ajax的响应,请使用以下代码。

$.post("print.php",{
        desc: $desc,
    }, function(data,success){
        alert(success);
        w=window.open(null, 'Print_Page', 'scrollbars=yes'); 
        w.document.write(data); //where data = response of print.php, the invoice
        w.document.close();
        w.print();
    }
);

NOTE : I'm assuming that if I open print.php in browser then I will get the invoice in a human readable manner, either a raw text or html whatever you will see by opening print.php in browser that will be printed as it is. 注意 :我假设如果我在浏览器中打开print.php,那么我将以一种人类可读的方式获取发票,无论是原始文本还是html,只要在浏览器中打开print.php就会看到,它将以它的形式打印出来是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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