这里我有一个表单和一些PHP代码只在单击提交按钮时运行。但是“IF”块一旦页面重新加载就运行。好像$ _POST数组不是空的。我可以尝试用单个元素,如!empty($_POST['name'])
。但我想测试通用的$ _POST数组。我该怎么做?
<?php
require_once 'input.php';
if(Input::exists()){
print_r($_POST);
$arr=array('ball');
echo $arr[0];
}
?>
<form action='' method='POST'>
name:<input type='text' name='name' ></br>
<input type='submit' value='submit'>
</form>
input.php文件:
class Input{
public static function exists($type='post'){
switch($type){
case 'post':
return (!empty($_POST))?true:false;
break;
default:
return (!empty($_GET))?true:false;
brek;
}
}
}
答案 0 :(得分:0)
为什么需要Input类?就这样做:
if ($_POST['name']) {
print_r($_POST);
$arr=array('ball');
echo $arr[0];
}
答案 1 :(得分:0)
我建议您为提交按钮添加名称
<input type='submit' name='submit' value='submit'> if(isset($_POST['submit'])) { php code goes here this will only run when submit btn has been clicked }
答案 2 :(得分:0)
action
属性将Input
类改为看起来像这样......
class Input {
public static function exists($type='post') {
if ($type == 'post' && count($_POST) > 0)
return true;
else if ($type == 'get' && count($_GET) > 0)
return true;
}
}
我坦率地讨厌切换语句,这应该可以正常工作。再一次,我认为Input
类太过分了。