zl程序教程

您现在的位置是:首页 >  前端

当前栏目

AJAX用户唯一性验证实现代码

AJAX代码 实现 用户 验证 唯一性
2023-06-13 09:14:14 时间
从数据库my中的username用户表里验证:
checkusername.html:
复制代码代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>无标题文档</title>
</head>
<scriptlanguage="javascript">
varxmlHttp;
functioncreateXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=newActiveXObject("microsoft.XMLHTTP");
}
elseif(window.XMLHttpRequest){
xmlHttp=newXMLHttpRequest();
}
}
functionsend_request(url,data){
createXMLHttpRequest();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=check_lll;
xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xmlHttp.send("username="+data);
}
functioncheck_lll(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
alert(xmlHttp.responseText);
}
}
}
functioncheck_username(){
varf=document.form1;
varusername=f.username.value;
if(username==""){
alert("NULL");
returnfalse;
}
else{
send_request("check_it.php",username);
}
}
</script>
<body>
<formid="form1"name="form1"method="post"action="">
<p> </p><p>
姓名:<inputtype="text"name="username"/>
</p>
<inputtype="button"value="checkit"onclick="check_username()"/>
<p> </p>
<p> </p>
</form>
</body>
</html>

check_it.php:
复制代码代码如下:

<?php
$username=$_POST["username"];
$conn=mysql_connect("localhost:3306","root","123");
mysql_select_db("my",$conn);
$sql="select*fromusernamewhereusername="$username"";
$result=mysql_query($sql,$conn);
$num=mysql_fetch_array($result);
if($num>0){
printf("can"tuse");
}
else{
printf("Itcanuse");
}

?>