km3945

一个表单数据检查思路....

 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"><!--
 7a, a:link{text-decoration: none; color:#000000; font-size:9pt;}    a:visited{text-decoration: none; color:#000000;}    a:hover{text-decoration: underline; color:red;}
 8body, 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*/
27if($_SERVER['REQUEST_METHOD'== 'POST')
28{
29    checkPost($_POST);
30    echo '<pre>'.print_r($_POST, true).'</pre>';
31}
32
33function 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
45function 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>

posted on 2006-05-10 17:55  KM3945  阅读(264)  评论(0)    收藏  举报

导航