我有一个要提交的表单,因此当我单击“提交”时,它会转到selectorpage.php并找到所选的函数类型,例如在此登录,进一步调用 Controller 执行该功能。我遇到的问题是js中有一个名为validateForm()的函数,一旦我单击提交按钮,它就会转到selectorPage.php。我想停止表单提交,通过js执行验证,然后从那里提交表单,我使用了 onsubmit = return false;在表单标签中,但它只是阻止了进一步执行任何操作的表单。而且我也不知道如何将表单重定向到selectorPage(如果它在js中以某种方式工作)。因此,任何人都想告诉我如何从 js 提交表单,然后将该页面重定向到selectorPage.php。谢谢
<form method="post" action="selector.php?type=login" id="login" id="loginForm">
<div class="row">
<div class="offset1 span1">
<div class="lbel">
<label class="control-label" for "loginName">
Username/Email
</label>
</div>
<div class="lbl_inpuCnt">
<input type="text" class="input-xlarge" id="loginName"
name="loginName" maxlength="50"/>
</div>
<div id="usernameError"> </div>
<div class="lbel">
<label class="control-label" for="loginPassword">
Password
</label>
</div>
<div class="controls">
<input type="password" class="input-xlarge"
id="loginPassword" name="loginPassword"
maxlength="50"/>
</div>
<div id="passwordError"> </div><br/>
</div>
</div>
<div style="margin-left: 55px;">
<input class="btn" style="width: 80px;" type="reset"
name="reset" value="Reset"/>
<input class="btn" style="width: 80px;" type="submit"
name="submit" value="Login" onclick="validateForm();"/>
</div>
</form>
这是根据上面代码的 JavaScript
function validateForm(){
form = document.forms['loginForm'];
if(document.getElementById('loginName').value == "")
document.getElementById('usernameError').innerHTML = 'Invalid username or email';
else{
document.getElementById('usernameError').innerHTML = " ";
form.submit();
}
} //suppose it for the email validation only for the time being
最佳答案
你可以试试
<form ... onsubmit="return validateForm();"
在validateForm()
函数中使用
return true / false
取决于是否发现错误。
关于php - 表单在 php 验证之前提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11489892/