Submit form via ajax; Return method if javascript is disabled?

I have a registration form in which the user provides email, re-enters the email address, password, re-enters the password, month of birth, day, year.

User clicks "Register".

If (Javascript is Enabled) { submit the form using ajax } elseif (Javascript is Disabled) { automatically fallback to traditional methods. } 

Note. Keep in mind that if the user fills out the form correctly, both methods must confirm that the email address does not yet exist in the database.

+4
source share
1 answer

There is a clean HTML / JS approach you can take to achieve this. Use the onsubmit event onsubmit in your form. If it is executed, it means that JS is enabled. Just return false from it to prevent normal submission. If JS is disabled, the form will usually be submitted via the action attribute.

 <form action="nonAjaxSubmit.php" onsubmit="return ajaxSubmit();">...</form> <script> function ajaxSubmit() { // Submitting through ajax return false; } </script> 

onsubmit not invoked if you programmatically call form.submit() from javascript.

+5
source

All Articles