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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作