使用PHP检查表单元素是否已发送但为空

时间:2012-02-22 15:01:13

标签: php forms post isset

我想分开两件事。

如果发送$_POST["something"],则为空。

OR

如果未发送$_POST["something"]

有办法吗?

5 个答案:

答案 0 :(得分:4)

您可能很想使用empty(),但如果您的可能值包含0,则会在您脸上爆炸。最好使用类似的东西:

if (isset($_POST['something'])) {
   // the form field was sent ...
   if ($_POST['something'] === '') {
      // the form field was sent, but is empty
   }
}

这是有效的,因为来自_GET / _POST / _REQUEST超全球的所有数据都是一个字符串,无论它是否为数字。

答案 1 :(得分:2)

您需要使用isset()

if (isset($_POST["something"])){
    echo "The Post Variable 'something' was posted";
    if (strlen($_POST["something"])==0)
        echo "The Post Variable is blank";
    else
        echo "The Post Variable Contains ".$_POST['something'];
}

答案 2 :(得分:2)

关注issetempty

http://php.net/manual/fr/function.isset.php

http://php.net/manual/fr/function.empty.php

编辑:以下评论属实。空应该用于空字符串,而不是数值,浮点值或字符串中的零。但是,如果您需要将这些作为有效值接受,这是一个函数:

function is_blank($value) {
    return empty($value) && !is_numeric($value);
}

答案 3 :(得分:0)

我使用php函数isset()来检查是否发送了一个POST

http://php.net/manual/en/function.isset.php

答案 4 :(得分:-1)

if($ _POST [“something”]) 如果有这样的字段,将返回true

如果$ _POST [“something”] ==“” 将完成剩下的工作