duan117890 2015-04-13 05:19
浏览 44
已采纳

如何动态检查$ _POST变量为NULL

How to loop through $_POST variables sent by a form to make sure none of them are empty or NULL. For example, if a form has 15 fields and is submitted. How to check that each of those 15 fields actually contained something other than NULL. I have multiple Forms but one validator class so I cannot specify the variable in the post array that need to be checked for each form.

I tried using foreach but that seemed to clear the $_POST array or corrupt it for some reason? The not working code is given below. If I var_dump($_POST) before I use the foreach and after the foreach I get an empty array after, but the correct values before. So I thing foreach is out of the question. Any Ideas?

foreach($_POST as $key => $value)
{
if ($value==NULL)
header ("Location: index.php")
}
  • 写回答

4条回答 默认 最新

  • dongxiangshen7916 2015-04-13 05:29
    关注

    You will not get any NULL values in $_POST array. If the form is submitted successfully and if the HTML form was not tampered with then all the fields will at least have some value or they will be empty ''. Do not use empty() function of PHP because it even considers 0 and a few other values as empty which might actually be the value. I believe this would be the correct way to write your code:

    foreach ($_POST as $key => $value) {
        if ($value === '')
            header("Location: index.php");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型