一个ajax的验证例子
时间:2007-10-15
来源:互联网
客户端页面check.html
复制PHP内容到剪贴板
<html>
<head>
<title>Ajax</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject) {
xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp = new window.XMLHttpRequest();
}
}
function sendData(what){
createXMLHttpRequest();
if(what == 'username'){
var datas = document.getElementById("username");
var URI = "check.php?username="+escape(datas.value);
}else if(what == 'email'){
var datas = document.getElementById("email");
var URI = "check.php?email="+escape(datas.value);
}
xmlHttp.open("GET",URI,true);
if(what == 'username')
xmlHttp.onreadystatechange = callback1;
else if(what == 'email')
xmlHttp.onreadystatechange = callback2;
xmlHttp.send(null);
}
function callback1(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var reback = xmlHttp.responseText;
document.getElementById("resultusername").innerHTML = reback;
}
}
}
function callback2(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var reback = xmlHttp.responseText;
document.getElementById("resultemail").innerHTML = reback;
}
}
}
</script>
</head>
<body style="text-align:center;font-family:arial;">
<table style="margin:0 auto; border:1px black solid;width:400px;font-size:12px;">
<tr>
<td>Name:</td>
<td>
<input type="text" name="username" id="username" />
</td>
<td><span id="resultusername" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<input type="text" name="email" id="email" />
</td>
<td><span id="resultemail" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
</tr>
</table>
</body>
</html>
服务端页面check.php
PHP代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<title>Ajax</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject) {
xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp = new window.XMLHttpRequest();
}
}
function sendData(what){
createXMLHttpRequest();
if(what == 'username'){
var datas = document.getElementById("username");
var URI = "check.php?username="+escape(datas.value);
}else if(what == 'email'){
var datas = document.getElementById("email");
var URI = "check.php?email="+escape(datas.value);
}
xmlHttp.open("GET",URI,true);
if(what == 'username')
xmlHttp.onreadystatechange = callback1;
else if(what == 'email')
xmlHttp.onreadystatechange = callback2;
xmlHttp.send(null);
}
function callback1(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var reback = xmlHttp.responseText;
document.getElementById("resultusername").innerHTML = reback;
}
}
}
function callback2(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var reback = xmlHttp.responseText;
document.getElementById("resultemail").innerHTML = reback;
}
}
}
</script>
</head>
<body style="text-align:center;font-family:arial;">
<table style="margin:0 auto; border:1px black solid;width:400px;font-size:12px;">
<tr>
<td>Name:</td>
<td>
<input type="text" name="username" id="username" />
</td>
<td><span id="resultusername" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<input type="text" name="email" id="email" />
</td>
<td><span id="resultemail" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
</tr>
</table>
</body>
</html>
复制PHP内容到剪贴板
<?php
$readyUserName = array(
"Tony",
"Jay",
"Jacket",
"songsong",
"Rose",
"Tom"
);
$userName = $_GET['username'];
$email = $_GET['email'];
if($userName != ""){
if(in_array($userName,$readyUserName)){
echo 'This name is exist...';
}else{
echo 'You can use this name...';
}
}
if($email != ""){
$model = "/[a-zA-Z0-9_]{1,}@[a-zA-Z0-9_]{1,}\.[a-zA-Z0-9_]{1,}[a-zA-Z0-9_.]{0,}/";
if(preg_match($model,$email)){
echo 'This email is right...';
}else{
echo 'This email is wrong...';
}
}
?>
PHP代码:
<?php
$readyUserName = array(
"Tony",
"Jay",
"Jacket",
"songsong",
"Rose",
"Tom"
);
$userName = $_GET['username'];
$email = $_GET['email'];
if($userName != ""){
if(in_array($userName,$readyUserName)){
echo 'This name is exist...';
}else{
echo 'You can use this name...';
}
}
if($email != ""){
$model = "/[a-zA-Z0-9_]{1,}@[a-zA-Z0-9_]{1,}\.[a-zA-Z0-9_]{1,}[a-zA-Z0-9_.]{0,}/";
if(preg_match($model,$email)){
echo 'This email is right...';
}else{
echo 'This email is wrong...';
}
}
?>
作者: liexusong 发布时间: 2007-10-15
简单,实用.
作者: samsung 发布时间: 2007-10-15
同楼上,简单,实用!
作者: hacksec 发布时间: 2007-10-15
很好的东西!谢谢。
作者: likai321 发布时间: 2007-10-15
在我本机上测试,不能用
作者: pets511 发布时间: 2007-12-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28