用ajax判断帐号是否存在(附例子)

来源:互联网 发布:ubuntu编译用什么版本 编辑:程序博客网 时间:2024/06/04 23:30

用ajax判断帐号是否存在

//html部分

<input type="text" name="username" id="username" class="wenbenkuang" onBlur="checkname()"/><div id="checkbox"></div>




//JS部分

<pre name="code" class="javascript">var XHR;//头文件可以不用修改function createXHR(){        if(window.ActiveXObject)       {       XHR=new ActiveXObject('Microsoft.XMLHTTP');       }       else if(window.XMLHttpRequest)       {       XHR=new XMLHttpRequest();       }}////检查部分function checkname(){      var username=document.getElementById('username').value;//从网页输入框里面获取输入的帐号      var useridRegex = /[\w\u4e00-\u9fa5]{3,15}/; //这个正则表达式网上找到的      var msg =" ";      //先对获取的帐号进行格式判断      if(username == null || username == "")        {        msg="<font color='#FF6699'>帐号不能为空!</font>";        }       else if(!useridRegex.test(username))       {        msg ="<font color='#FF6699'>帐号格式不正确!</font>";      }      else      {      createXHR();       XHR.open("POST","checkname.php?id="+username,true);//跳转到checkname.php      XHR.onreadystatechange=byhongfei;      XHR.send(null);      }      document.getElementById('checkbox').innerHTML=msg;}/////返回部分function byhongfei(){//readyState0~4的状态===0: 请求未初始化1: 服务器连接已建立2: 请求已接收3: 请求处理中4: 请求已完成,且响应已就绪     if(XHR.readyState == 4)     {          if(XHR.status == 200)           {            var textHTML=XHR.responseText;           document.getElementById('checkbox').innerHTML=textHTML;////返回的值显示在特定的地方。           }     }}

 

/////////checkname.php部分

<?php  header("content-Type: text/html; charset=utf-8");  include("conn.php");$ch_name = $_GET['id'];$sql="select * from ajax where name='$ch_name'";  $query=mysql_query($sql);  //$num = mysql_num_rows($query);if(is_array(mysql_fetch_array($query)))     {//输出结果(返回值)  echo "<font color='#FF6699'>帐号已存在</font>";     }  else    {  echo "<font color='#00EE76'>帐号可以使用</font>";     }  <span style="font-size:24px;">?>  </span>

//conn.php部分

<span style="font-size:14px;"><?php   header("content-Type: text/html; charset=utf-8");   $link =@mysql_connect("127.0.0.1","root","");   if(!$link)     {     die('Could not connect: ' . mysql_error());     }     mysql_query("set names utf8",$link);      mysql_select_db("ajax",$link); ?></span>


ajax例子:http://download.csdn.net/detail/alpha_xiao/9596317



0 0
原创粉丝点击