1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><meta http-equiv="content-language" content="zh-CN" /><meta http-equiv="pragma" content="no-cache" /><meta http-equiv="expires" content="0" /><meta http-equiv="MSThemeCompatible" content="Yes" /><meta http-equiv="imagetoolbar" content="no" />
3
<meta http-equiv="widow-target" content="_top" /><meta name="robots" content="index, follow" /><meta name="author" content="3945, ljm77@km169.net" />
4
<meta name="keywords" content="" /><meta name="description" content="" /><meta name="copyright" content="Copyright 3945 All Rights Reserved" />
5
<title>无标题文档</title>
6
<style type="text/css"><!--
7
a, a:link{text-decoration: none; color:#000000; font-size:9pt;} a:visited{text-decoration: none; color:#000000;} a:hover{text-decoration: underline; color:red;}
8
body, td, p, li, div, select{font-size:9pt; font-family:"宋体";}
9
--></style>
10
</head>
11
12
<body>
13
14
15
<?php
16
/*
17
检测表单数据,
18
一至以来都想做个方便一点的表单数据检查程序.但看来看去没有什么好方法,
19
这是今天想到的.利用表单元素的NAME属性.
20
我们在定义表单元素时,把NAME的属性定义成带数据类型的格式如:
21
<input type="text" name="test_s_n" />
22
该IPNUT是s[String],n[noNull],test就是表单的表意词.
23
意思就是TEST是字符串,不能为空.
24
有些东西想的不是太清楚.只是一种思路.
25
下面是一个小DEMO
26
*/
27
if($_SERVER['REQUEST_METHOD'] == 'POST')
28
{
29
checkPost($_POST);
30
echo '<pre>'.print_r($_POST, true).'</pre>';
31
}
32
33
function checkPost($data)
34
{
35
foreach($data as $key=>$val)
36
{
37
list($name, $type) = explode('_', $key, 2);
38
if(!is_null($type))
39
{
40
if(!ck($val, $type)) die($key .'类型不匹配!');
41
}
42
}
43
}
44
45
function ck($value, $type)
46
{
47
$a =array();
48
$types = explode('_', $type);
49
foreach($types as $t)
50
{
51
if('s' == $t)
52
{
53
$a[$t] = is_string($value);
54
}
55
if('i' == $t)
56
{
57
$a[$t] = is_numeric($value);
58
}
59
if('n' == $t)
60
{
61
$a[$t] = !empty($value);
62
}
63
}
64
return in_array(true, $a);
65
}
66
?>
67
<form name="form1" method="post" action="">
68
<input type="text" name="test_s_n" />
69
<input type="submit" value="Submit" name="b1" />
70
<input type="reset" value="Reset" name="b3" />
71
</form>
72
</body>
73
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">2
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><meta http-equiv="content-language" content="zh-CN" /><meta http-equiv="pragma" content="no-cache" /><meta http-equiv="expires" content="0" /><meta http-equiv="MSThemeCompatible" content="Yes" /><meta http-equiv="imagetoolbar" content="no" />3
<meta http-equiv="widow-target" content="_top" /><meta name="robots" content="index, follow" /><meta name="author" content="3945, ljm77@km169.net" />4
<meta name="keywords" content="" /><meta name="description" content="" /><meta name="copyright" content="Copyright 3945 All Rights Reserved" />5
<title>无标题文档</title>6
<style type="text/css"><!--7
a, a:link{text-decoration: none; color:#000000; font-size:9pt;} a:visited{text-decoration: none; color:#000000;} a:hover{text-decoration: underline; color:red;}8
body, td, p, li, div, select{font-size:9pt; font-family:"宋体";}9
--></style>10
</head>11

12
<body>13

14

15
<?php16
/*17
检测表单数据,18
一至以来都想做个方便一点的表单数据检查程序.但看来看去没有什么好方法,19
这是今天想到的.利用表单元素的NAME属性.20
我们在定义表单元素时,把NAME的属性定义成带数据类型的格式如:21
<input type="text" name="test_s_n" />22
该IPNUT是s[String],n[noNull],test就是表单的表意词.23
意思就是TEST是字符串,不能为空.24
有些东西想的不是太清楚.只是一种思路.25
下面是一个小DEMO26
*/27
if($_SERVER['REQUEST_METHOD'] == 'POST')28
{29
checkPost($_POST);30
echo '<pre>'.print_r($_POST, true).'</pre>';31
}32

33
function checkPost($data)34
{35
foreach($data as $key=>$val)36
{37
list($name, $type) = explode('_', $key, 2);38
if(!is_null($type))39
{40
if(!ck($val, $type)) die($key .'类型不匹配!');41
}42
}43
}44

45
function ck($value, $type)46
{47
$a =array();48
$types = explode('_', $type);49
foreach($types as $t)50
{51
if('s' == $t)52
{53
$a[$t] = is_string($value);54
}55
if('i' == $t)56
{57
$a[$t] = is_numeric($value);58
}59
if('n' == $t)60
{61
$a[$t] = !empty($value);62
}63
}64
return in_array(true, $a);65
}66
?>67
<form name="form1" method="post" action="">68
<input type="text" name="test_s_n" />69
<input type="submit" value="Submit" name="b1" />70
<input type="reset" value="Reset" name="b3" />71
</form>72
</body>73
</html>
本文介绍了一种利用PHP进行表单数据验证的方法,通过在表单元素的NAME属性中加入特定标记来指示数据类型及是否允许为空,从而实现简单有效的表单验证。
2955

被折叠的 条评论
为什么被折叠?



