时间:2021-07-01 10:21:17 帮助过:10人阅读
比如一个表单,很多个选项,怎么样来用php来判断值是否为空或者为正确数值
<form action="add.php" method="post"><br data-filtered="filtered"> <input type="text" name="name"><br data-filtered="filtered"> <input type="text" name="name2"><br data-filtered="filtered"> <input type="text" name="name3"><br data-filtered="filtered"> <input type="text" name="name4"><br data-filtered="filtered"> <input type="text" name="name5"><br data-filtered="filtered"> </form><br data-filtered="filtered"> <br data-filtered="filtered"> <?php<br data-filtered="filtered"> $name1=$_POST[name1];<br data-filtered="filtered"> $name2=$_POST[name2];<br data-filtered="filtered"> $name3=$_POST[name3];<br data-filtered="filtered"> $name4=$_POST[name4];<br data-filtered="filtered"> $name5=$_POST[name5];<br data-filtered="filtered"> <br data-filtered="filtered"> //插入数据库,如果是就单独判断一个变量,可以用IF,那如果多个怎么办呢<br data-filtered="filtered"> if($name1==''){<br data-filtered="filtered"> echo '请输入数据';<br data-filtered="filtered"> exit;<br data-filtered="filtered"> }<br data-filtered="filtered"> <br data-filtered="filtered"> $sql="insert into book values('$name1','$name2','$name3','$name4','$name5')";<br data-filtered="filtered"> mysql_query($sql);<br data-filtered="filtered"> <br data-filtered="filtered"> ?>
一个一个判断。empty判空。
//设$_POST = array('name' => '', 'name2' => 'e','name3' => 'r','name4' => '','name5' => ' '); $t = array_keys($_POST, '');//如果空格不算空,就用这条 $t = array_keys(array_map('trim', $_POST), '');//如果空格算空,就用这条 if($t) { //有空数据项 print_r($t); //值就是为空的项}
if($name1=='' || $name2="" || $name3=""){ echo '请输入数据'; exit; }
<form action="add.php" method="post"> <input type="text" name="name[]"> <input type="text" name="name[]"> <input type="text" name="name[]"> <input type="text" name="name[]"> <input type="text" name="name[]"> </form> <?php //这里的$name将会得到一个数组 $name=$_POST['name']; foreach ($name as $value) { if($value=="") { echo "不能有空值"; exit(); } } $sql="insert into book values('$name[0]','$name[1]','$name[2]','$name[3]','$name[4]')"; mysql_query($sql); ?>
这个觉得好用
循环数组,判断值是否为空就可以了。
单个获取表单值,if判断是否为空。