php – 即使没有填写所有字段,表单也会提交

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 即使没有填写所有字段,表单也会提交脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我是PHP新手,我不太明白为什么这个表单在所有情况下提交…
所以我的问题是:

如何修复此问题,以便表单仅在用户填写所有字段时提交?

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

我用了两个&&和||然而,无论您填写什么领域,它总是提交.

<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" />

在此先感谢您的帮助,这似乎是一个很棒的社区参与!

解决方法

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 即使没有填写所有字段,表单也会提交全部内容,希望文章能够帮你解决php – 即使没有填写所有字段,表单也会提交所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。