用户登录
用户注册

分享至

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

  • 作者: 达?矢抾哆拉?
  • 来源: 51数据库
  • 2021-08-22
从数据库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.51sjk.com/Upload/Articles/1/0/270/270312_20210708032950833.jpg">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<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");
}

?>
软件
前端设计
程序设计
Java相关