利用ajax实现简单的注册验证局部刷新实例

时间:2024-05-02 17:04:53 

1,ajax(asynchronouse javascript and xml)异步的 javascrip 和xml
2,(包含了7种技术:javascript xml xstl dom xhtml css xmlhttpRequest)
3,是一种与服务器语言无关的技术,可以用在(php/jsp/asp.net)
4,ajax的工作原理: 创建一个ajax引擎->发送数据给服务器--》通过回调函数接收数据---》将数据赋给指定的页面

下面是注册验证案例register。php是注册页面。registerProcess.php用于接收数据并返回数据

register.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<!-- 告诉浏览器不要缓存 -->
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
<!--
function sendRequest(){
//创建一个ajax引擎
var xmlHttpRequest;
//不同浏览器获取xmlRequest对象方法不一样
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("MMMMMictofrt");
}else{
xmlHttpRequest = new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest
function checkName(){
myXmlHttpRequest = sendRequest();
if(myXmlHttpRequest){
//window.alert("创建成功");
//第一个参数表示请求方式get.post。第二个参数指定url,对哪个页面发送ajax请求
//用get方式提交的数据如果没有发生变化的话浏览器将会从缓存中直接提取
var url = "/ajax_yhyz/registerProcess.php? username=getValue('username').value";
//1,用这样就可以不从缓存提取了
//var url = "/ajax_yhyz/registerProcess.php? mytime='new Date()' & username=getValue('username').value";
//2,<meta http-equiv="pragma" content="no-cache">告诉浏览器不要缓存
//window.alert(url); 用于测试url是否成功
//第三个参数ture 表示异步机制
myXmlHttpRequest.open("get",url,true);
//指定回调函数,chul是函数名,表示如果状态发生变化就调用该函数,有四个状态
myXmlHttpRequest.onreadystatechange = chuli;
//发送请求,如果是get请求则填写null即可
//额如果是post请求,则填写实际的数据
myXmlHttpRequest.send(null);
}
}
function chuli(){
//window.alert("处理函数被回调");
if(myXmlHttpRequest.readyState == 4){
//window.alert("服务器返回"+myXmlHttpRequest.responseText);
getValue("myres").value = myXmlHttpRequest.responseText;
}
}
function getValue(id){
//return document.getElementById(id).value; 这样做的话没办法完成局部刷新
return document.getElementById(id);
}
-->

</script>
</head>

<body>
<form action="???" method="post">
用户名字:<input type="text" name="username1" id="username" ><input type="button" onclick="checkName()" value="验证用户名">
<input type="text" id="myres" />
<br/>
用户密码:<input type="password" name="password"><br>
电子邮件:<input type="text" name="email"><br/>
<input type="submit" value="用户注册">
</form>
<form action="???" method="post">
用户名字:<input type="text" name="username2" >

<br/>
用户密码:<input type="password" name="password"><br>
电子邮件:<input type="text" name="email"><br/>
<input type="submit" value="用户注册">
</form>
</body>
</html>


regiseterProcess.php


<?php
$username=$_REQUEST['username'];
if($username=="shunping"){
echo "err";
}else{
echo "ok";
}
?>
标签:ajax,局部刷新,注册验证
0
投稿

猜你喜欢

  • Python使用pymongo库操作MongoDB数据库的方法实例

    2023-06-04 06:20:22
  • MySQL中UPDATE语句使用的实例教程

    2024-01-27 00:10:40
  • 11个Python3字典内置方法大全与示例汇总

    2021-11-26 22:33:51
  • 浅谈Go语言中的次方用法

    2024-02-17 04:57:08
  • python学习笔记之调用eval函数出现invalid syntax错误问题

    2023-11-03 01:48:30
  • js正则表达式验证密码强度【推荐】

    2024-04-29 13:39:30
  • MySQL给字符串加一个高效索引的实现

    2024-01-18 17:13:34
  • pytorch中的model=model.to(device)使用说明

    2023-02-23 15:07:48
  • Python标准异常和异常处理详解

    2021-02-23 05:10:36
  • 详解Idea 2020 找不到或无法安装官方汉化包解决方案

    2022-03-21 23:01:29
  • 图片放大镜jquery.jqzoom.js使用实例附放大镜图标

    2024-04-30 08:51:05
  • PHP错误Warning: Cannot modify header information - headers already sent by解决方法

    2023-11-15 11:53:16
  • MySQL的指定范围随机数函数rand()的使用技巧

    2024-01-17 15:25:50
  • 浅谈Django学习migrate和makemigrations的差别

    2021-05-11 03:36:11
  • 简单了解Django应用app及分布式路由

    2023-08-30 19:29:47
  • python中数据爬虫requests库使用方法详解

    2022-04-01 05:47:11
  • 24个实用JavaScript 开发技巧

    2024-04-10 11:03:39
  • Asp函数介紹(37个常用函数)

    2011-04-11 11:06:00
  • MySQL利用procedure analyse()函数优化表结构

    2024-01-17 14:51:00
  • python关于集合的知识案例详解

    2021-01-25 05:11:28
  • asp之家 网络编程 m.aspxhome.com