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 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件