通过ajax提交表格;如果javascript被禁用,则回退方法?

时间:2022-09-19 09:13:31

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.

注意:请记住,如果用户正确填写表单,则两种方法都需要验证数据库中是否已存在该电子邮件地址。

1 个解决方案

#1


5  

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.

您可以采用纯HTML / JS方法来实现这一目标。在表单中使用onsubmit事件处理程序。如果它执行它意味着JS已启用。只是从中返回false以防止正常提交。如果禁用JS,表单将通过action属性正常提交。

<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.

如果您以编程方式从javascript调用form.submit(),则不会调用onsubmit。

#1


5  

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.

您可以采用纯HTML / JS方法来实现这一目标。在表单中使用onsubmit事件处理程序。如果它执行它意味着JS已启用。只是从中返回false以防止正常提交。如果禁用JS,表单将通过action属性正常提交。

<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.

如果您以编程方式从javascript调用form.submit(),则不会调用onsubmit。