简体   繁体   English

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

[英]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. 注意:请记住,如果用户正确填写表单,则两种方法都需要验证数据库中是否已存在该电子邮件地址。

There's a pure HTML/JS approach you can take to achieve this. 您可以采用纯HTML / JS方法来实现这一目标。 Use an onsubmit event handler in your form. 在表单中使用onsubmit事件处理程序。 If it executes it means that JS is enabled. 如果它执行它意味着JS已启用。 Just return false from it to prevent normal submission. 只是从中return false以防止正常提交。 If JS is disabled, the form will submit normally through the action attribute. 如果禁用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM