php中_POST获取值, 不存在时,如何验证

WBOY
Release: 2016-06-13 13:35:38
Original
1168 people have browsed it

php中_POST获取值, 不存在时,怎么验证


测试php










$name = "";
if(!empty($_POST["name"]))
{
  $name = $_POST["name"];
}
print $name;
?> 

运行时报错, 开始就没有$_POST["name"]值, 怎么验证有没有$_POST["name"]值.
如果有name值提交就打印值, 没得就打印""

------解决方案--------------------
报什么错? 你的也行。用isset()也行。
------解决方案--------------------
test.php改成:

if (isset($_POST)) {
$name = "";
if(!empty($_POST["name"]))
{
$name = $_POST["name"];
}
print $name;
} else {
echo "$_POST not Set!";
}

?>
------解决方案--------------------
很好奇,这段代码回报什么错?
------解决方案--------------------
错误级别打开过高了吧,关闭错误提示就不会报了。这个在生产环境下可用,开发还是严谨一些,养成验证的好习惯比较好。

 推荐用楼上说的isset()函数验证一下值是否存在。

比如想知道是否存在name值的提交,可以这样:

PHP code
if(isset($_POST['name'])||!empty($_POST['name'])
{
  echo $_POST['name'];//值存在且不为空打印出来;
}else{
  echo " ";//值不存在或为空,打印空"";
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!