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

本文介绍了一种利用PHP进行表单数据验证的方法,通过在表单元素的NAME属性中加入特定标记来指示数据类型及是否允许为空,从而实现简单有效的表单验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值