·您当前的位置:首页 > 技术教程 > jQuery教程 >

[jQuery]ajax在php程序无刷新判断用户名

时间:2017-05-02 00:05酷播
[jQuery]ajax在php程序无刷新判断用户名

[jQuery]ajax在php程序无刷新判断用户名  
register.php

  1. <html> 
  2. <head> 
  3. <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
  4. <title>用户注册</title> 
  5. <script type="text/javascript"> 
  6. //创建ajax引擎 
  7. function getXmlHttpObject(){ 
  8.     //不同的浏览器获取xmlhttprequest 的方法不一样 
  9.     if(window.ActiveXObject){ 
  10.         //window.alert('ie'); 
  11.         xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP"); 
  12.         } 
  13.     else{ 
  14.         //window.alert('ff'); 
  15.         xmlHttpRequest=new XMLHttpRequest(); 
  16.         } 
  17.     return xmlHttpRequest; 
  18. } 
  19. var myXmlHttpRequest=""; 
  20. function checkName(){ 
  21.     myXmlHttpRequest=getXmlHttpObject();//1号线 
  22.     if(myXmlHttpRequest){ 
  23.         //window.alert("创建Ajax引擎成功!"); 
  24.         //通过myXmlHttpRequest对象发送请求到服务器的某一个页面 
  25.         //第一个参数表示请求的方式,get/post 
  26.         //第二个参数指定url,对那个页面发出ajax请求(本质仍然是http请求),只要是web开发的底层是http请求,http的底层是tcp/ip协议 
  27.         //第三个参数 true 表使用异步机制,false表示不使用异步机制。 
  28.         //打开请求  
  29.         //var url="/ajax/registerPro.php?username="+$("username").value; 
  30.         //var url="/ajax/registerPro.php?mytime="+new date()+"&usernameusername="+$("username").value; 
  31.         var url="/ajax/registerPro.php"; 
  32.         //这个是要发送数据 
  33.         var data="username="+$("username").value; 
  34.         //window.alert(url); 
  35.         myXmlHttpRequest.open("post",url,true); 
  36.         //这句话不可少 
  37.         //http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  38.         myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  39.         //指定回调函数 . chuli是函数名 
  40.         myXmlHttpRequest.onreadystatechange=chuli; 
  41.         //真的发送请求,如果是get请求发送null即可;如果是post请求,则填入实际的请求即可 
  42.         //myXmlHttpRequest.send(null); //2号线 
  43.         myXmlHttpRequest.send(data); //2号线 
  44.         } 
  45. } 
  46. //回调函数 
  47. function chuli(){ 
  48.  
  49.     //window.alert("回调函数成功!!"+xmlHttpRequest.readyState); 
  50.     //还是取值了 
  51.     if(xmlHttpRequest.readyState==4){ 
  52.             //window.alert("服务器返回"+xmlHttpRequest.responseText); 
  53.             $("myres").value=xmlHttpRequest.responseText; 
  54.         } 
  55. } 
  56. //这里写一个函数 
  57. function $(id){ 
  58.     return document.getElementById(id); 
  59.  } 
  60. </script> 
  61. </head> 
  62. <body> 
  63.     <form action="" method="post"> 
  64.             用户名字:<input type="text" onkeyup="checkName()" name="username1" id="username" />  
  65.             <input onclick="checkName();" type="button" value="验证用户名" /><input style="border-width:0px;color:red;" type="text" id="myres"/> <br />  
  66.             用户密码:<input type="password" name="password" /> <br />  
  67.             电子邮件:<input type="text" name="email" /> <br /> <input type="button" value="注册账号"> 
  68.     </form> 
  69. </body> 
  70. </html>  

registerPro.php

  1. $username = $_POST['username']; 
  2. // echo "用户名是:" . $username; // 3号线 
  3. if ($username == "zhosoft") { 
  4.     echo "此用户名不可用!";  // 注意,这里的数据是返回给请求的页面。 
  5. } else { 
  6.     echo "恭喜您,此用户名可用!"; 
  7. } 
  8.  
  9.   
  10. 作者:churk2012 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器