Search code examples
octobercmsoctobercms-backend

onSend function does not execute it's code when OctoberCMS AJAX calls it


i'm trying to execute some code using the October's Data Attribute API for front-end forms. The onSend function executes simple codes like echo 'something', but doesn't more complex code, like trying to use mpdf plugin. I guess it's related to AJAX reloading the page, but don't know how to solve it.

I tried using data-request from the Data Attribute API, calling the function onSend, but the code on onSend does not execute.

My form header is as follows:

<form class="register-form" method="POST" data-request="onSave">

and my function onSave:

public function onSave(){
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<div style="text-align: center"> Test </div>');
$mpdf->Output();
}

Note that both codes are on a component, and the onSave function executes when i try something just like:

echo 'Test';

Solution

  • You are trying to render / stream a PDF? Have you looked at the network monitor in your browser?

    Something I would test is your mpdf code with the public function onRun() call. Make sure that works.

    Try to update a partial. data-request-update="'{{__SELF__::partial}}', '#element'"

    Other than that you might need to do a full page redirect. How I stream PDFs is I have a routes.php that takes this https.domain.com/stream/pdf?id=encryptedsomething. The route is programmed to look up the specific object in the model after decrypting the id and displays the data in a PDF.