php 检查表单是否提交时字段为空
正如标题所示,我试图弄清楚如何在单击提交按钮后检查表单中是否有任何空条目。
if(isset($_POST['submit']) && ($selected == ''|| $text == '' || $email == ''))
{
// *do things*
}
else{
//*more things*
}
这是不正确的吗?
As the title says, I'm trying to figure out how to check if there were any null entries in a form after the submit button has been clicked.
if(isset($_POST['submit']) && ($selected == ''|| $text == '' || $email == ''))
{
// *do things*
}
else{
//*more things*
}
is this incorrect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像处理提交按钮一样引用它们。
即:
$_POST['input_name']
从那里使用适当的函数检查它:
isset()
、empty()
、is_null()
(尽管表单变量很少出现为null
)我也鼓励您阅读 PHP 外部变量。
You would reference them in the same way that you handled the submit button.
That is:
$_POST['input_name']
From there check it using the appropriate function:
isset()
,empty()
,is_null()
(although form variables rarely come across asnull
)I'd also encourage you to read up on PHP external variables.
您可以做的是循环 $_POST 变量。排除那些您不感兴趣的内容,并做出如下内容:
...然后您在 $allIsOk 上做出选择。
这种方法有两个原因:
对于 0 甚至“0”将返回 true,并且可能会导致头撞
问题。
制作一个巨大的 if 语句
当然,这只是想法。检查文档总是明智的。另外,您可以用 array_walk 调用替换 foreach 循环,以使事情变得更有趣(尤其是从 PHP 5.3 开始)。 ;-)
祝你好运!
PS 另外,为了查明您的脚本是否已被 POST 操作调用,我建议您使用 $_SERVER 全局变量,而不是考虑提交元素。 http://php.net/manual/en/reserved.variables.server.php 只需检查“REQUEST_METHOD”参数即可。
所以,你可能有:
What you could do is to loop over the $_POST variables. Exclude those in which you're not interested in, and make something like:
...and then you make your choice on $allIsOk.
This approach is for two reasons:
will return true for 0 or even "0" and might cause headbanging
problems.
making a huge if statement
Of course, this is just the idea. It's always wise to check documentation. Also, you could substitute the foreach cycle with an array_walk invocation to make things fancier (esp. from PHP 5.3 onwards). ;-)
Good luck!
PS Also, to find out whether your script has been invoked by a POST action, instead of taking into account the submit element, I suggest you to use the $_SERVER global. http://php.net/manual/en/reserved.variables.server.php Just check for the 'REQUEST_METHOD' parameter.
So, you might have: