服务器端的PHP表单验证
问题描述:
我有一个PHP表单,我需要提交4个字段数据。服务器端的PHP表单验证
- 名称
- 电子邮件
- 密码
- 移动
现在我要检查所有的上述数据是否可用。
为了调试的目的我是用$ _GET下面
if (!empty($_GET['name']) or !empty($_GET['email']) or !empty($_GET['password']) or !empty($_GET['mobile'])) {
$response = array(
'status' => 'OK',
'error' => '1',
'message' => 'Registered successfully. Please check your email for activation details',
'data' => '');
} else {
$response = array(
'status' => 'NOK',
'error' => '1',
'message' => 'There was some error please try again.',
'data' => '');
}
以上是不能够正确地验证表单。
答
它的条件,不or
使用and
是很重要的:
if (!empty($_GET['name']) and !empty($_GET['email']) and !empty($_GET['password']) and !empty($_GET['mobile'])) {
答
大多数形式使用method="POST"
这意味着在PHP端提交的,你应该使用$_POST
代替$_GET
。
您也有一个逻辑错误,只有在它们全部填满后才会继续,因此您想要使用and
而不是or
。
...问题是? – 2013-05-06 07:38:42
我无法获得这个工作 – 2013-05-06 07:39:08
使用[array_key_exists](http://php.net/manual/en/function.array-key-exists.php)而不是空的。也请看[filter_var function](http://nl1.php.net/filter_var) – HamZa 2013-05-06 07:41:35