AJAX 用户唯一性验证实现代码
程序员文章站
2022-03-14 10:39:21
从数据库my中的username用户表里验证: checkusername.html: 复制代码 代码如下:
从数据库my中的username用户表里验证:
checkusername.html:
<!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=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
var xmlhttp;
function createxmlhttprequest(){
if(window.activexobject){
xmlhttp = new activexobject("microsoft.xmlhttp");
}
else if(window.xmlhttprequest){
xmlhttp = new xmlhttprequest();
}
}
function send_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);
}
function check_lll(){
if(xmlhttp.readystate == 4){
if(xmlhttp.status == 200){
alert(xmlhttp.responsetext);
}
}
}
function check_username(){
var f = document.form1;
var username = f.username.value;
if(username == ""){
alert("null");
return false;
}
else{
send_request("check_it.php",username);
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="">
<p> </p><p>
姓名: <input type="text" name="username" />
</p>
<input type="button" value="check it" 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 * from username where username = '$username'";
$result = mysql_query($sql,$conn);
$num = mysql_fetch_array($result);
if($num > 0){
printf("can't use");
}
else{
printf("it can use");
}
?>
checkusername.html:
复制代码 代码如下:
<!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=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
var xmlhttp;
function createxmlhttprequest(){
if(window.activexobject){
xmlhttp = new activexobject("microsoft.xmlhttp");
}
else if(window.xmlhttprequest){
xmlhttp = new xmlhttprequest();
}
}
function send_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);
}
function check_lll(){
if(xmlhttp.readystate == 4){
if(xmlhttp.status == 200){
alert(xmlhttp.responsetext);
}
}
}
function check_username(){
var f = document.form1;
var username = f.username.value;
if(username == ""){
alert("null");
return false;
}
else{
send_request("check_it.php",username);
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="">
<p> </p><p>
姓名: <input type="text" name="username" />
</p>
<input type="button" value="check it" 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 * from username where username = '$username'";
$result = mysql_query($sql,$conn);
$num = mysql_fetch_array($result);
if($num > 0){
printf("can't use");
}
else{
printf("it can use");
}
?>
上一篇: xajax的FORM例子