3

I have two different pages that serve separate functions.

When a button is clicked, data is posted to the database through finalizecontract.php. contract.php is a TCPDF form that uses the data from the database to fill out and generate the signed version of the contract, and saves the PDF file to a folder on my server.

I already tested and verified that the output works properly. I'm trying to figure out how to run contract.php when the Finalize Contract button is clicked. I attempted to use $.get but it's not working (or I have it in the wrong place).

What is the proper method of posting the data to the database, then calling the TCPDF file to save the contract in a PDF form?

<script type="text/javascript">

     function finalizecontract() {
        // Add record
        $.post("ajax/finalizecontract.php", {
            uuid: $("#c_uid").val(),    
        }, function (data, status) {
            if(data != "Success")
            {
                alert(data);
                $.get('contract.php');
            }
            else
            {
                $("#finalize_contract_modal").modal("hide");

                location.reload(true);
            }
        });
    }
</script>
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
mike437
  • 77
  • 1
  • 8

1 Answers1

2

I think you may want to use the .done() chain on $.post()

$.post("ajax/finalizecontract.php", {
    uuid: $("#c_uid").val(),
}).done(function (data) {
    //I would `console.log(data)` here to confirm you are getting the expected values
    if(data != "Success") {
        alert(data);
        $.get('contract.php');
    } else {
        $("#finalize_contract_modal").modal("hide");
        location.reload(true);
    }
);

Docs (See final example) https://api.jquery.com/jquery.post/

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • It's still not running the contract.php file. The data is being posted to the database through finalizecontract.php, but contract.php isn't running because the PDF isn't being saved. I just confirmed again that contract.php works when I run it on it's own. – mike437 Apr 14 '18 at 02:50
  • What is `data` printing out as? Additionally you could add `ajax/finalizecontract.php` for more context. – Seth McClaine Apr 14 '18 at 02:53
  • It moved pretty quick because of the reload, but it said Success – mike437 Apr 14 '18 at 02:57
  • I just realized my stupid error... let me try it and see if it works. / edit: Yep... had it under the fail section, not the actual successful section. And now I look dumb, with which I certainly don't need any extra assistance. Thanks for your help! – mike437 Apr 14 '18 at 02:57
  • You can set your console to persist so it doesn't clear on reloads. https://stackoverflow.com/questions/5327955/how-to-make-google-chrome-javascript-console-persistent – Seth McClaine Apr 14 '18 at 02:59
  • I'd appreciate the upvote as well (giving you one) :-) – Seth McClaine Apr 14 '18 at 03:04