利用Ajax判断注册用户名是否已被注册

HTML代码部分:

文件名为:post.html

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="post.php" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="user_name" id="user"><span></span></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="user_pwd"></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
</tr>
</table>
</form>
</body>
<script>
var user = document.getElementById('user');

user.onblur=function(){
var user_name = user.value;
var ajax = false;

//1.根据浏览器的不同,使用不同的方法创建对象
if(window.ActiveXObject){
ajax = new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}

//2.创建请求
ajax.open('post','post.php',true);

//3.发送请求,send里面的参数只能通过post方法发送
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// ajax.send();当方式为get的时候用这个
ajax.send('user='+user_name);//当方式为post的时候用这个

//4.判断请求的状态和请求结果
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && ajax.status==200){
var message = ajax.responseText;
console.log(message);
if(message==1){
user.nextSibling.innerHTML='<font color="red">×,不可以注册</font>';
}else{
user.nextSibling.innerHTML='<font color="green">√,可以注册</font>';
}
}
}

}
</script>
</html>

PHP代码部分:

文件名为post.php

<?php 
header("Content-Type:text/html;charset:utf-8");
include_once('mysql_class.php');

$mysql = new mysql('localhost','root','','wdopro','utf8');
$user_name = $_POST['user_name'];
$sql = 'SELECT * FROM wdo_admin WHERE admin_name = "'.$user_name.'"';
$result = $mysql->get_one($sql);
if($result){
	echo 1;
}else{
	echo 0;
}

 ?>

另外在根目录下添加一个定义好的类(也就是php文件中引入的类)

mysql_class.php

关于这个类可以看我之前写的博客。





转载于:https://my.oschina.net/arin/blog/648342

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值