Having problems with Ajax function and tcpdf output()

时间:2015-06-26 09:53:47

标签: javascript php jquery ajax tcpdf

I am using TCPDF to create a pdf from form fields. The problem I have is that the php script runs fine and creates the file but ajax fails even though the script has executed fine.

My ajax

$(document).ready(function() {

    $("#make-pdf").click(function() {
        // Set variables from form data

        $.ajax({
            url: "generate-pdf.php",
            type: "POST",
            dataType: "json",
            data: { pdf_name: $('#pdf-name').val(), salutation: $('#salutation').val(), client_name: $('#client-name').val(), client_location: $('#client-location').val(), client_email: $('#client-email').val(), reason_for_saving: $('#reason-for-saving').val(), advisor_name: $('#advisor-name').val(), advisor_email: $('#advisor-email').val(), advisor_contact: $('#advisor-contact').val(), brokerage_name: $("brokerage-name").val() },
            success: function(){
                 alert('success');
            },
            error:function(){
                  alert('error');
            }

        });
    });
});

The end of my php file

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

    $pdf->Output(__DIR__ .'/pdfs/'.$pdf_name.'.pdf', 'F');

    return true;

    //============================================================+
    // END OF FILE
    //============================================================+

}
else
{
    return "Incorrect request";
}

?>

The function saves the file like it is supposed to, but I'm only getting a failed response from the Ajax call. Nothing is being logged in the console either and the page seems to reload

1 个答案:

答案 0 :(得分:0)

问题出在html按钮的位置。我没有意识到<button>代码中的<form>会提交表单,即使它不是类型提交