PHP - proper check if $_POST['variable'] is posted

时光毁灭记忆、已成空白 提交于 2019-12-01 22:14:07

isset($_POST['submit']) checks if the submit key is set in the $_POST array. It doesn't just check whether the $_POST array exists and is therefore not "pointless". If you want to check whether the value is not falsey (== false), which includes 0, without triggering an error, that's what empty is for:

if (!empty($_POST['submit']))

which is the same thing as

if ($_POST['submit'])

but without triggering a notice should the value not exist.

See The Definitive Guide To PHP's isset And empty for an exhaustive explanation.

sumit

Try

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

As per my understanding, It should be like below:

if (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST'){
  # if method is post, code goes here.
}

and if you are sure that your method is POST for sure. and you have data post in $_POST you can use code like below:

if (isset($_POST['submit']) && $_POST['submit'] != '') {# I think, '' instead of 0
  # if data is posted, code goes here.
}

I usually prefer $_POST.

Cyril Joudieh

$_POST[] checks to see if a variable is submitted and not the form name.

if ( isset($_POST['name']) ) {
    // work here
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!