Simulate submit form using jquery ajax call

I have a piece of code that defines a div and some callbacks when clicked. To avoid code redundancy, I want to reuse it. Callbacks use $.post calls to communicate with the server, whereas for this particular page, I want the page to refresh as the form passes.

Is there any difference in using the submit to submitPage.php and using the following?

 $.post('submitPage', dataParams, null, 'json'). success(function(resp, status, req) { window.location = 'submitPage.php'; // redirect }); 

I am curious how this could essentially affect the processing of records in dataParams on submitPage.php.

+4
source share
1 answer

The difference is that when you submit the form, the page request usually also contains the data serialized with it. You submit the form as an AJAX request, and then redirect the user to the page without the data attached to it with the serialized form.

So, if you need the $_POST variable, which will be available in submitPage.php , I would let the form send normally, rather than welcome the submission using an AJAX request and then redirect the user.

You can add the $_POST data to $_SESSION and then use this when redirecting the user, but if you have no reason for this, it seems like an unnecessary bit of code.

+1
source

Source: https://habr.com/ru/post/1383721/


All Articles