PHP+ajax注册进度条验证代码

来源:互联网 发布:C语言unlink函数 编辑:程序博客网 时间:2024/05/26 05:52
ajax.php页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
var xmlHttp;
function S_xmlhttprequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function funphp100(username){
var username=document.reg.username.value;

S_xmlhttprequest();
xmlHttp.open("GET","checkuserreg.php?id="+username,true);
    xmlHttp.onreadystatechange=byphp;
  //确定发送请求方式,URL,及是否同步执行下段代码
xmlHttp.send(null);
}
//处理返回信息的函数
function byphp(){
   if(xmlHttp.readyState==1){ 
   document.getElementById('check1').innerHTML="loading........"; //可以调用图片
    }
if(xmlHttp.readyState==4){      //判断对象状态
if(xmlHttp.status==200){   //信息已成功返回,开始处理信息
var byphp100=xmlHttp.responseText;
document.getElementById('check1').innerHTML=byphp100;
}
}
}
/*可以用相应的js控件验证*/
/*function check(){    //用户名为空的时候
    if(document.reg.username.value==""){
    document.getElementById('check').innerHTML=" <font color=red>用户名不能为空!</font>";
         document.reg.username.focus();
         return false;
    }
    if(document.getElementById('check').innerHTML==" <font color=red>The number is registed</font>"){    //用户名已被注册的时候(<font color=red>The number is registed</font>是AJAX返回回来的)
    document.reg.username.focus();
         return false;
    }
     if(document.reg.userpwd.value==""){     //密码为空的时候
           document.getElementById('pwd').innerHTML=" <font color=red>用户密码不能为空!</font>";
           document.reg.userpwd.focus();
           return false;
         }
         if(document.reg.userpwd.value.length<6){    //密码长度错误的时候
           document.getElementById('pwd').innerHTML=" <font color=red>密码长度不能小于6位!</font>";
           document.reg.userpwd.focus();
           return false;
         }

         if (document.reg.reuserpwd.value==""){ //没有再次输入密码的时候
           document.getElementById('repwd').innerHTML=" <font color=red>请再输入一次密码!</font>";
           document.reg.reuserpwd.focus();
           return false;
         }
         if(document.reg.userpwd.value!=document.reg.reuserpwd.value){   //再次输入密码不正确的时候
           document.getElementById('repwd').innerHTML=" <font color=red>两次输入的密码不一致!</font>";
          document.reg.reuserpwd.focus();
           return false;
         }
         return true;   //上面的验证都通过则return true,页面信息将被POST到checkuserreg.php进行处理。
}*/
</script>
<body>
 <form method="post" name="reg" id="reg" action="" onsubmit="return funphp100()">
                 <table cellpadding="0" cellspacing="0" border="0" width="100%">
                         <tr>
                                 <td class="td1">登陆帐号:</td>
                                 <td>
                                         <input name="username" type="text" id="username" size="20" maxlength="20" class="textipt" onBlur="funphp100()"/>
                                         <span id="check" class="msg">4-16个字符,英文小写、汉字、数字。</span></td>
                         </tr>
                         <tr>
                                 <td class="td1">登录密码:</td>
                                 <td><input name="userpwd" type="password" id="userpwd" size="30" maxlength="30" class="textipt"/>
                                   <span class="msg" id="pwd">密码字母有大小写之分,英文、数字。</span></td>
                         </tr>
                         <tr>
                                 <td class="td1">确认密码:</td>
                                 <td><input name="reuserpwd" type="password" id="reuserpwd" size="30" maxlength="30" class="textipt"/> <span class="msg" id="repwd">请再次输入登录密码。</span></td>
                         </tr>
                         <tr>
                                 <td colspan="2" align="left" valign="middle" height="50">
                                  <span id="check1" class="msg">qqqqq</span>
                 <input name="submit" type="submit" value="提交" /></td>
                         </tr>
                 </table>
                 </form>
</body>
</html>
checkuserreg.php 页面
<?php 
header("Content-type: text/html;charset=utf-8");
if($_GET['id'])
{
sleep(2);
$conn=mysql_connect("localhost","root","") ;
mysql_select_db("2012",$conn) ;
 $username=$_GET['id'];//获取注册名
$sql=mysql_query("select * from db_admin where username='$username'");
if(is_array(mysql_fetch_row($sql))){
echo" <font color=red>The number is registed</font>";
}else{
echo" <font color=red>恭喜,此用户名可以注册!</font>";
}
}
?>
原创粉丝点击