Onsubmit with JavaScript and PHP

1

I'm learning a bit with JavaScript and PHP.

The case is that I have a form ( form ). Once filled in you have two processes.

One is PHP, which collects the $POST of the html with if(isset($_POST['submit'])) and processes them to export it to text to a local fixed route automatically (no download).

Once it's over, it throws a header to the results page.

And on the other hand, I have a onsubmit in form , which triggers a JavaScript, which collects the entire document and generates a PDF using JSPDF. This same JavaScript, uses JQuery - AJAX to send the information to another processing PHP and I store it locally (automatically without headers , it saves it in a specific route).

The concrete issue is, in doing so, in two ways, many times one ends before the other and the processing of either does not end and generates a corrupt file or fails to generate it (PDF corrupt is the usual).

The question is, how could I shoot so that I would first do the part with java and then the if(isset($_POST['submit'])) of PHP?

Or some way to unify the process and not go 'each one on his own'.

There's something I do not know, like an event or similar ...

Partially I solved the problem in the first part, doing a sleep before sending the header to the results page (wait x seconds), so just process the second part and without problem, but not what I see optimal.

Any help is appreciated. Thanks.

    
asked by nano_9219 13.07.2018 в 10:00
source

2 answers

1

If what you need is to run the Javascript and then the PHP, change the submit by a button so that the form is not sent when you press it. In this button you call the function that you need Javascript to create the file

<input type='button' onclick='function()'>

At the end of the javascript function you send the form to PHP

$('#id_form').submit()

With this until it does not finish running all the Javascript will not start the PHP.

    
answered by 13.07.2018 / 11:25
source
0

Using sleep is not a successful solution. I can not think of any way to work two different paths from two different technologies (PHP / Javascript) so what I would do would be the following:

  • I would only run the onsubmit and create a single workflow in Javascript.
  • Create the PDF file
  • Once the file is created, we send the serialized form information to the PHP file via AJAX (you can use a callback from the previous function)
  • In this way we have a single workflow and each action is executed in a linear way.

        
    answered by 13.07.2018 в 10:23