doubi4617 2017-01-14 11:12
浏览 369

在JQuery之后调用PHP文件

I am using DOMPDF and I want my form input text to be available in PDF format. So when I type something in input fields, and I press export to PDF, I want it to be there. So here is how I planned my solution. The problem appears when I call the PHP with POST from HTML because It does not update the input values. It just sends the blank ones. I need to change that so it updates them.

1) Fill in the input fields with text 2) Click Export to PDF 3) Use JQuery to change the HTML with input values 4) Call the PHP file ($dompdf->loadHtml(file_get_contents('index.php'));

I tried using AJAX for this but it doesn't work. I DON'T need to send ANY data to the PHP File. I just need to call if after the JQuery changed the HTML.

Here's my PHP File:

<?php
require_once 'autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(file_get_contents('index.php'));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render('doc.pdf');

// Output the generated PDF to Browser
$dompdf->stream();

?>

And my JQUERY File

function ajax() {
    alert('in');
$.ajax({
    url: 'jobsheet.php',
    type: 'POST',
    data: formData,
    success:function(response){
        alert(response);
    }
});
}

If you have any other solution for this, please let me know!

  • 写回答

1条回答 默认 最新

  • dongzanghui4624 2017-01-23 18:07
    关注

    I see two problems with your technique.

    First, Dompdf (as of 0.6.1) does not parse PHP so you will need to pre-parse that before passing it off to dompdf. You can use output buffering for that:

    <?php
    ob_start();
    require_once 'index.php';
    $html = ob_get_clean();
    ob_end_clean();
    
    require_once 'autoload.inc.php';
    use Dompdf\Dompdf;
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream();
    ?>
    

    Second, if all you want to do is display the results why use AJAX at all? It requires significantly more work to fetch and display the PDF document this way when you could simply use a form post. If you have a particular need for using AJAX you should specify.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退