php 检查表单是否提交时字段为空

发布于 2024-12-06 14:07:36 字数 221 浏览 0 评论 0原文

正如标题所示,我试图弄清楚如何在单击提交按钮后检查表单中是否有任何空条目。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-12-13 14:07:36

您可以像处理提交按钮一样引用它们。

即: $_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 as null)

I'd also encourage you to read up on PHP external variables.

樱花落人离去 2024-12-13 14:07:36

您可以做的是循环 $_POST 变量。排除那些您不感兴趣的内容,并做出如下内容:

$allIsOk = true;
foreach ($_POST as $index => $value) {
  if (strlen($value)<1) {
    $allIsOk = false;      
  }
}

...然后您在 $allIsOk 上做出选择。

这种方法有两个原因:

  1. 根据上面的建议,您需要结合检查,因为empty()
    对于 0 甚至“0”将返回 true,并且可能会导致头撞
    问题。
  2. 通过这种方法,您可以添加参数,而无需
    制作一个巨大的 if 语句

当然,这只是想法。检查文档总是明智的。另外,您可以用 array_walk 调用替换 foreach 循环,以使事情变得更有趣(尤其是从 PHP 5.3 开始)。 ;-)

祝你好运!

PS 另外,为了查明您的脚本是否已被 POST 操作调用,我建议您使用 $_SERVER 全局变量,而不是考虑提交元素。 http://php.net/manual/en/reserved.variables.server.php 只需检查“REQUEST_METHOD”参数即可。

所以,你可能有:

if ('POST' == $_SERVER['REQUEST_METHOD']) {

  // It's ok for these to be null - or empty
  $exclude = array('submit', 'other_param');
  $allIsOk = true;
  foreach ($_POST as $index => $value) {
    if (!in_array($index, $exclude) && strlen($value)<1) {
      $allIsOk = false;      
    }
  }
}

if ($allIsOk) {
  // Do Something
} else {
  // Do Something Else
}

What you could do is to loop over the $_POST variables. Exclude those in which you're not interested in, and make something like:

$allIsOk = true;
foreach ($_POST as $index => $value) {
  if (strlen($value)<1) {
    $allIsOk = false;      
  }
}

...and then you make your choice on $allIsOk.

This approach is for two reasons:

  1. With suggestion above, you need to combine checks, since empty()
    will return true for 0 or even "0" and might cause headbanging
    problems.
  2. With this approach you can add parameters, without
    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:

if ('POST' == $_SERVER['REQUEST_METHOD']) {

  // It's ok for these to be null - or empty
  $exclude = array('submit', 'other_param');
  $allIsOk = true;
  foreach ($_POST as $index => $value) {
    if (!in_array($index, $exclude) && strlen($value)<1) {
      $allIsOk = false;      
    }
  }
}

if ($allIsOk) {
  // Do Something
} else {
  // Do Something Else
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文