目前我使用注册表单并希望在其中查找一个 php 验证类实现以进行验证检查,我在这里找到了SchizoDuckie 提供的一个类,我的表单将使用 ajax 请求来调用文件reg_process.php
,我的问题是我该怎么做使用此类来验证必填字段?如果发现无效输入,则向浏览器发送错误消息?
这是 reg_process.php:
<?php
include('FormValidator.php');
$email = mysql_real_escape_string(strtolower(trim($_POST['email'])));
$password = mysql_real_escape_string(trim($_POST['password']));
$rpassword = mysql_real_escape_string(trim($_POST['rpassword']));
$fname = mysql_real_escape_string($_POST['fname']);
$contact = mysql_real_escape_string($_POST['contact']);
$validations = array(
'name' => 'anything',
'email' => 'email',
'alias' => 'anything',
'pwd'=>'anything',
'gsm' => 'phone',
'birthdate' => 'date');
$required = array('name', 'email', 'alias', 'pwd');
$sanatize = array('alias');
$validator = new FormValidator($validations, $required, $sanatize);
if($validator->validate($_POST))
{
$_POST = $validator->sanatize($_POST);
// now do your saving, $_POST has been sanatized.
die($validator->getScript()."<script type='text/javascript'>alert('saved changes'); </script>");
}
else
{
die($validator->getScript());
}
?>
阿贾克斯调用:
$('#btn_reg').click(function(){
var parameters = $('#reg_form').serialize();
$.ajax({
url: 'reg_process.php',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(data){
//how to get error if found invalid
}
});
});