user1880826
user1880826

Reputation: 81

Submit form via ajax; Fallback method if javascript disabled?

I have a registration form where a user provides email, re-enter email, password, re-enter password, birth month, day, year.

The user clicks "Sign Up."

If (Javascript is Enabled) {

submit the form using ajax

} elseif (Javascript is Disabled) {

automatically fallback to traditional methods.

}

Note: Keep in mind that if a user properly fills out the form, both methods need to verify that the email address does not already exist in the database.

Upvotes: 4

Views: 1739

Answers (1)

Xavi López
Xavi López

Reputation: 27880

There's a pure HTML/JS approach you can take to achieve this. Use an onsubmit event handler in your form. If it executes it means that JS is enabled. Just return false from it to prevent normal submission. If JS is disabled, the form will submit normally through the action attribute.

<form action="nonAjaxSubmit.php" onsubmit="return ajaxSubmit();">...</form>

<script>
    function ajaxSubmit() { 
        // Submitting through ajax
        return false;
    }
</script>

Take into account onsubmit is not called if you programmatically call form.submit() from javascript.

Upvotes: 5

Related Questions