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
Form submission after Javascript Validation
提问by Enigma Crack
I want to submit my form to a PHP
file called reg.php
after validating it using JavaScript
. The HTML
form 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 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.
}
}
回答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 else
like:
并且只需添加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 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
演示
回答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 action
attribute to the reg.php
file:
在这里,您需要获取表单元素和submit()
它,因此您需要将 id 添加到表单并将action
属性添加到reg.php
文件:
<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>