在接收表单数据时进行合法性检验是确保应用安全的关键步骤。下面详细介绍如何在 PHP 类中实现全面的数据验证。
1. 基本验证方法
1.1 使用 filter_var 函数
class UserValidator {
public function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
public function validateUrl($url) {
return filter_var($url, FILTER_VALIDATE_URL) !== false;
}
public function sanitizeInput($input) {
return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}...
在 PHP 类中接收表单提交的数据有多种方法,下面介绍几种常见的实现方式:
1. 通过构造函数接收
class FormHandler {
private $formData;
public function __construct($postData) {
$this->formData = $postData;
}
public function processForm() {
// 处理表单数据
$name = $this->formData['name'] ?? '';
$email = $this->formData['email'] ?? '';
echo "姓名: $name, 邮箱: $email";
}
}
// 使用方式
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$formHandler = new F...
<form method="post" id="sform" name="sform" action="do.php?x=save">
<table width="100%" cellpadding="10" cellspacing="1" align="center" border="0" bgcolor="#A8BCF3">
<tr bgcolor="#ffffff">
<td>
<span id="imgSelectBox"><input type="file" id="imgsrc" name="imgsrc" value="" /></span> <input type="hidden" id="uploadimg" name="uploadimg" value="" />
</td>
</tr>
<tr bgcolor="#ffffff">
<td>
<input type="submit" id="sSubmit" name="sSubmit" value="保存记录" />
</td>
...
1 javascript ,设置一个变量,只允许提交一次。 <script language="javascript"> var checkSubmitFlg = false; function checkSubmit() { if (checkSubmitFlg == true) { return false; } checkSubmitFlg = true; return true; } document.ondblclick = function docondblclick() { window.event.returnValue = false; } document.onclick = function doconclick() { if (checkSubmitFlg) { window.event.returnValue = false; } } </script> <html:form action="myAction.do" method=&qu...
直接上代码,我就不多写了。。。以下是HTML代码:【复制】
<form method="post" name="form1" action="test.php"><input type="text" id="a" name="a" size="" maxlength="" value="Ajax测试" /> <input type="button" value="Ajax Test" onclick="AjaxTest(this.form);" /></form>以下是Javascript代码:【复制】
<script type="text/javascript" src="prototype.js"></script><script type="text/javascript"> < !--...
1.取消按钮按下时的虚线框 在input里添加属性值 hideFocus 或者 HideFocus=true 2.只读文本框内容 在input里添加属性值 readonly 3.防止退后清空的TEXT文档(可把style内容做做为类引用) <INPUT style=behavior:url(#default#savehistory); type=text id=oPersistInput> 4.ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9" > 5.只能为中文(有闪动) <input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keyCode==13)event.keyCode=9"> 6.只能为数字(有闪动) <input onkeyup="...
类别:网页编程 查看:112
更新:2014-06-02
:selected 返回值:Array<Element(s)>
概述
匹配所有选中的option元素
示例
描述:
查找所有选中的选项元素
HTML 代码:
<select>
<option value="1">Flowers</option>
<option value="2" selected="selected">Gardens</option>
<option value="3">Trees</option>
</select>
jQuery 代码:
$("select option:selected")
结果:
[ <option value="2" selected="selected">Gardens</option> ]
...
:disabled 返回值:Array<Element(s)>
概述
匹配所有不可用元素
示例
描述:
查找所有不可用的input元素
HTML 代码:
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
jQuery 代码:
$("input:disabled")
结果:
[ <input name="email" disabled="disabled" /> ]
...
:checked 返回值:Array<Element(s)>
概述
匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option)
示例
描述:
查找所有选中的复选框元素
HTML 代码:
<form>
<input type="checkbox" name="newsletter" checked="checked" value="Daily" />
<input type="checkbox" name="newsletter" value="Weekly" />
<input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
</form>
jQ...
:enabled 返回值:Array<Element(s)>
概述
匹配所有可用元素
示例
描述:
查找所有可用的input元素
HTML 代码:
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
jQuery 代码:
$("input:enabled")
结果:
[ <input name="id" /> ]
...
:hidden 返回值:Array<Element(s)>
概述
匹配所有不可见元素,或者type为hidden的元素
示例
描述:
查找隐藏的 tr
HTML 代码:
<table>
<tr style="display:none"><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:hidden")
结果:
[ <tr style="display:none"><td>Value 1</td></tr> ]
描述:
匹配type为hidden的元素
HTML 代码:
<form>
<input type="text" name="email"...
:file 返回值:Array<Element(s)>
概述
匹配所有文件域
示例
描述:
查找所有文件域
HTML 代码:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
&l...
:button 返回值:Array<Element(s)>
概述
匹配所有按钮
示例
描述:
查找所有按钮.
HTML 代码:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<...
:image 返回值:Array<Element(s)>
概述
匹配所有图像域
示例
描述:
匹配所有图像域
HTML 代码:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
&...
:reset 返回值:Array<Element(s)>
概述
匹配所有重置按钮
示例
描述:
查找所有重置按钮
HTML 代码:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
...
:submit 返回值:Array<Element(s)>
概述
匹配所有提交按钮
示例
描述:
查找所有提交按钮
HTML 代码:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>...