Skip to content
Advertisement

No response from PHP on AJAX request

I am working on a simple signup page using jQuery and PHP using AJAX.

Here is the script for making the ajax call:

JavaScript

and the PHP script which responds to the call:

signup.php:

JavaScript

using PDO.

dbconnect.php:

JavaScript

Problem

Neither of the two responses are being returned to the jQuery ajax call. I checked with console.log() but nothing shows up in the browser. Data gets stored in the MySQL database (checked it) which means that execute() is working fine in PHP, but still no alert is shown in the browser. 1 does get returned in case count evaluates to 0.

Also, when I refresh the form without actually submitting the form, I get the error POST 412 (Precondition Failed) at the browser console.

Apache access log

JavaScript

Form HTML markup

JavaScript

Advertisement

Answer

Since your form doesn’t contain a url to send to and your onSubmit-Handler (submitForm) doesn’t return false, the form will actually get posted by the browser to the page it’s currently on (which is most likely not capable of processing the form’s POST data) and will just reload the login-form containing page.

The POST will likely be send to your ajax script as well, but your browser won’t wait for the reponse, because it already moved on.

See https://html.spec.whatwg.org/multipage/forms.html#concept-form-submit for further details on the submit process.

Solution would be to either add return false to your submitForm function or to the onSubmit handler (;return false)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement