PHP POST vs JavaScript AJAX Request – Comparison and Use Cases

ajaxjavascriptPHP

I have been into web development with both client amd server side code, and have been using JavaScript Ajaxto receive and manipulate for data along with PHP scripts.

However, I noticed that websites like Google and Facebook use PHP forms instead of sending ajax requeststo PHP scripts.

What I wonder is that what is the advantage of direct PHP form submit over sending form data from a static HTML page to a PHP Script via JavaScript Ajax, especially from the UX and security point of view?

Best Answer

What are PHP forms? Do you mean HTTP POST requests (Wikipedia, MDN)?

Ajax will usually POST as well. The only difference is that Ajax runs in the background, and does not navigate to a new URL. That also answers the UX question: if the user intention is not to leave that page, a background request seems more sensible.

There are no special security considerations for either case. PHP can be used both securely and insecurely, but this does not depend on the type of request.