Javascript 验证后的表单提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32410590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:13:28  来源:igfitidea点击:

Form submission after Javascript Validation

javascriptphphtmlformsvalidation

提问by Enigma Crack

I want to submit my form to a PHPfile called reg.phpafter validating it using JavaScript. The HTMLform is given below:

我想提出我的表格到一个PHP文件名为reg.php使用验证之后JavaScript。该HTML形式给出如下:

HTML Form:

HTML 表单:

<form method="post">
   <input type="text" id="username" name="username"></input>        
   <button type="button" onclick="val()">Sign Up</button>
</form>

JavaScript Validation

JavaScript 验证

The following is the JavaScriptvalidation code:

以下是JavaScript验证码:

function val(){

    var uname = document.getElementById('username').value;
    if(!uname){
        document.getElementById("error").innerHTML = "Enter a username";
        return false;
    }
    else{
        // I want to submit form if this else statement executes.
    }


}

回答by SHAZ

Just add action="reg.php"in <form>and return val() functions on onclick, also make type="submit"to form submission, like:

只需添加action="reg.php"<form>和返回VAL()上的功能onclick,也使type="submit"形成的提交,如:

<form method="post" action="reg.php">
   <input type="text" id="username" name="username"></input>        
   <button type="submit" onclick="return val()">Sign Up</button>
</form>

and just add return true;in elselike:

并且只需添加return true;else这样的:

function val(){

    var uname = document.getElementById('username').value;
    if(!uname){
        document.getElementById("error").innerHTML = "Enter a username";
        return false;
    }
    else{
        return true;
    }
}

回答by Mohammed Elhag

HTML Code

HTML代码

<form method="post" onsubmit="return val()">
     <input type="text" id="username" name="username">       
     <button type="submit" >Sign Up</button>
 </form>

Javascript Code

Javascript代码

function val(){

     var uname = document.getElementById('username').value;
     if(!uname){
         document.getElementById("error").innerHTML = "Enter a username";
         return false;
     }
     else{
         return true;
     }
}

回答by Dhaval Marthak

You can submit it with form namein else section.

您可以name在其他部分使用表单提交。

document.myForm.submit();


 var uname = document.getElementById('username').value;
 if (!uname) {
      document.getElementById("error").innerHTML = "Enter a username";
      return false;
 } else {
      document.myForm.submit(); // Form will be submitted by it's name
 }

Markup

标记

<form method="post" name="myForm">.....</form>

Demo

演示

回答by michelem

Here is, you need to get the form element and submit()it, so you need to add the id to the form and the actionattribute to the reg.phpfile:

在这里,您需要获取表单元素和submit()它,因此您需要将 id 添加到表单并将action属性添加到reg.php文件:

JSFiddle

JSFiddle

<form method="post" id="theForm" action="reg.php">
   <input type="text" id="username" name="username"></input>        
   <button type="button" onclick="val()">Sign Up</button>
</form>

JS:

JS:

function val() {
    var uname = document.getElementById('username').value;
    if (!uname) {
        document.getElementById("error").innerHTML = "Enter a username";
        return false;
    } else {
        document.getElementById('theForm').submit();
    }
}

回答by XanT

You can put a return true statement in the else block and then change the form to this.

您可以在 else 块中放置一个 return true 语句,然后将形式更改为此。

<form method="post" action="reg.php">
   <input type="text" id="username" name="username"></input>        
   <button type="submit" onclick="return val()">Sign Up</button>
</form>