即使所有字段未填写,表单也会提交 [英] Form submits even when all fields aren't filled out

查看:84
本文介绍了即使所有字段未填写,表单也会提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PHP,我不明白为什么这个表单在任何情况下都提交...
所以我的问题是:

如何解决这个问题,以便表单只在用户填写所有字段时提交?

  if(!$ _ POST ['用户名']&&!$ _ POST ['password']&&!$ _ POST ['repassword'] 
&&!$ _ POST ['user_firstname']&&!$ _ POST ['user_lastname']){
header('Location:register.php?msg =您没有完成所有必填字段');
}

我使用了&& || 运算符,但它总是提交,无论填写什么字段。

 < form action =createuser.phpmethod =postname =registration_formid =registration_form> ; 
< label>电子邮件< / label>
< input name =usernametype =textid =usernamesize =50maxlength =50/>< br />
< label>名字< / label>
< input name =user_firstnametype =textid =user_firstnamesize =50maxlength =50/>< br />
< label>姓氏< / label>
< input name =user_lastnametype =textid =user_lastnamesize =50maxlength =50/>< br />
< label>密码< / label>
< input name =passwordtype =passwordid =passwordsize =50maxlength =100/>< br />
< label>重新输入密码< / label>
< input name =repasswordtype =passwordid =repasswordsize =50maxlength =100/>< br />


< input type =submitvalue =Registername =submit/>

在此先感谢您的帮助,这似乎是一个非常棒的社区! p>

解决方案

  if(empty($ _ POST ['username'])
|| empty($ _ POST ['password'])
|| empty($ _ POST ['repassword'])
|| empty($ _ POST ['user_firstname'])
|| empty( $ _POST ['user_lastname'])
){
header('Location:register.php?msg =您没有完成所有必填字段');

$ / code>

编辑:正确,如评论所示,验证需要更充实比这个。检查后重定向不是一个好主意,而是尝试将表单发布到自身。如果没有任何错误,那么继续处理表单数据,如果有错误将它们设置在变量/对象/数组中,并将它们显示在表单上/用表单数据重新填充表单。


I'm new to php and I don't quite understand why this form is submitting under all circumstances... So my question is:

How do I fix this so the form only submits when the user fills out all fields?

if (!$_POST['username'] && !$_POST['password'] && !$_POST['repassword'] 
&& !$_POST['user_firstname'] && !$_POST['user_lastname'] ){
header('Location: register.php?msg=You did not complete all of the required fields');
}

I used both && and || operators however it always submits no matter what field you filled out.

<form action="createuser.php" method="post" name="registration_form" id="registration_form">
<label>Email</label>
<input name="username" type="text" id="username" size="50" maxlength="50" /><br />
<label>First Name</label>
<input name="user_firstname" type="text" id="user_firstname" size="50" maxlength="50" /><br />
<label>Last Name</label>
<input name="user_lastname" type="text" id="user_lastname" size="50" maxlength="50" /><br />
<label>Password</label>
<input name="password" type="password" id="password" size="50" maxlength="100" /><br />
<label>Re-type Password</label>
<input name="repassword" type="password" id="repassword" size="50" maxlength="100" /><br />


<input type="submit" value="Register" name="submit" />

Thanks in advance for any help, and this seems like an awesome community to be involved in!

解决方案

if (empty($_POST['username'])
    || empty($_POST['password'])
    || empty($_POST['repassword'])
    || empty($_POST['user_firstname'])
    || empty($_POST['user_lastname'])
    ){
header('Location: register.php?msg=You did not complete all of the required fields');
}

EDIT: True, as the comments suggest, the validation needs to be more substantial than this. redirecting after checking is not a good idea, instead try posting the form to itself. If there aren't any errors then continuing with processing the form data, if there are errors set them in a variable/object/array and display them on the form/repopulate the form with the post data.

这篇关于即使所有字段未填写,表单也会提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆