第一种: 使用submit提交
<form method = "post" action = "jieshou.php">
.....
<input type="submit" />
</form>
这一种是最常见到的提交方式,在jieshou.php接受数据就可以了
第二种: 使用button提交
function form1() {
//获取form
var form1 = document.getElementById("form1");
//设置action
form1.action = "hello.php";
//提交form表单
form1.submit();
}
【特别注意的一点是:】
这里的值,默认是以GET方式提交到hello.php页面,也就是说你不需要在js代码里面去处理传递值的问题,只需要在hello.php接收就行,看截图,代码跟上面一样的
第三种: 使用超链接提交
<a href="hello.html?username=123456">使用超链接提交</a>
这种也是经常看到的,比如说,查看个人信息,链接会带这个人的id过去。在hello.php页面用GET方式去接收数据就可以了
【总结:】
如果说,想要在提交之前对数据进行一个验证时,可以这样子做。
1)使用submit按钮提交,但是在form表单里面加onsubmit = “return checkUser();”
<form method = 'post' action = 'hello.php' onsubmit = "return checkUser();" >
function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("userpassid").value;
if(result == "" ){
alert("用户名不能为空");
return false;
}
if(password == "" ){
alert("密码不能为空");
return false;
}else{
return true;
}
}
return false是验证不通过的时候阻止浏览器进行提交的。一定要记住是onsubmit,如果是用onclick的话,无论验证是否通过,它都会提交的哦
2)使用button按钮提交
也就是在action之前进行一系列的验证,然后不通过就不给予提交,直接上代码
<input type="button" value="" class="btn2" onclick = "checkUser();" />
function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("passid").value;
if(result == "" ){
alert("用户名不能为空");
return false;
}
if(password == "" ){
alert("密码不能为空");
return false;
}
//获取form
var form1 = document.getElementById("form1");
form1.action = "hello.php";
form1.submit();
}