简体   繁体   English

Javascript 验证后的表单提交

[英]Form submission after Javascript Validation

I want to submit my form to a PHP file called reg.php after validating it using JavaScript .我想在使用JavaScript验证后将我的表单提交到名为reg.phpPHP文件。 The HTML form is given below: 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 JavaScript validation 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.
    }


}

Just add action="reg.php" in <form> and return val() functions on onclick , also make type="submit" to form submission, like:只需在<form>添加action="reg.php"并在onclickreturn val()函数,同时使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;并添加return true; in else like:else如:

function val(){

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

You can submit it with form name in 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演示

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;
     }
}

Here is, you need to get the form element and submit() it, so you need to add the id to the form and the action attribute to the reg.php file:在这里,您需要获取表单元素并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();
    }
}

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>

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

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