문제

The idea is I am sending data to an external php via ajax, where that external php will create a pdf using tcpdf, and then return that pdf to me. But something's wrong.

I can't see where the mistake lies. Basically, I'm trying to send two pieces of information via ajax. The first is a full string of html markup which I assign to it using the .html() function. The second is just a test to see if it would also get sent or not.

The JQuery looks like this (the problem lies in the PDF button):

$(".pdf-report").dialog({
    autoOpen: false,
    modal: true,
    draggable: true,
    resizable: true,
    width: 700,
    height: 650,
    dialogClass: "no-close",
    buttons: {
        "PDF": function(){
            /*
            var send_html = $('#query_result').html();
            $.post('pdfajax.php',
                {
                    qch: 'TESTQCH',
                    code: send_html
                },
                function( data ){
                    $('#report_result').html(data); // assuming there is a div with id result
                }
            );
            */
            var send_html = $('#report_result').html();
            alert(send_html);
            $('#ireport_result').prop('src', 'pdfajax.php?send_html='+send_html);

        },
        "Close": function() {
            $(this).dialog("close");
        }
    }
});

And the external file, pdfajax.php, looks like this:

    require_once 'core/init.php';
require_once 'tcpdf/tcpdf.php';

$html = input::get('send_html');

//echo 'test ',var_dump($qch),'<br />',var_dump($html);

$method = $_SERVER['REQUEST_METHOD'];
//echo $method;
if(strtolower($method) == 'post'){

$pdf = new TCPDF();
$pdf->AddPage();

$pdf->writeHTML($html, true, false, false, false, 'C');


$pdf->Output('example_001.pdf', 'I');

I know nothing gets sent because this is the result (the blank iframe, not the text above it): enter image description here

Oh yes and the input class has the get function which simply says this:

    public static function get($item){
    if(isset($_POST[$item])){
        return $_POST[$item];
    } elseif (isset($_GET[$item])){
        return $_GET[$item];
    }
    return '';
}

EDIT:

Here's the HTML

<div class="pdf-report" title="Report Dialog">
<br />
<div id="report_result"></div>
<iframe src="tcpdf/examples/example_001.php" id="ireport_result"></iframe>
<br />

도움이 되었습니까?

해결책

I dont think, there is an issue with your ajax call. Your $.post appears to be fine. But the issue might be because of your success callback handler.

You are setting the pdfajax.php as the source of your iframe after your ajax request has completed successfully, which doesn't make any sense. Because, this would trigger a new GET request to the URL 'pdfajax.php' and it wouldn't contain any request parameters at all.

Ideally, your data object would be containing the response received from 'pdfajax.php' which you can use to display within a div or so.

function( data ){
    $('#report_result').html(data); // this can be used for html response not for pdf.
}

But in case, you want the data in your iframe, i don't think there is a need for Ajax. You can directly set the src for the iframe as shown below.

    $(".pdf-report").dialog({
    autoOpen: false,
    modal: true,
    draggable: true,
    resizable: true,
    width: 700,
    height: 650,
    dialogClass: "no-close",
    buttons: {
        "PDF": function(){
            var send_html = $('#report_result').html();
            $('#ireport_result').prop('src', 'pdfajax.php?code='+send_html);
        },
        "Close": function() {
            $(this).dialog("close");
        }
    }
});

And your php file has few issues. The request parameter is code not send_html and your request type is GET not POST.

require_once 'core/init.php';
require_once 'tcpdf/tcpdf.php';
$html = input::get('code');

$method = $_SERVER['REQUEST_METHOD'];
if(strtolower($method) == 'get'){
    $pdf = new TCPDF();
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, false, false, 'C');
    $pdf->Output('example_001.pdf', 'I');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top