php ajax用户注册检测代码_PHP教程

原创
2016-07-20 11:17:51 579浏览

实只要简单的实现ajax的检测用户名,正规点要分三个文件。我这里简单点:

第一个:index.php




无标题文档








用 户 名*




第二个要用到js:ajax.js
[php]

var xmlHttp;
function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveX0bject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlHttp)
{
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
}
}


function startRequest(username)
{
createXMLHttpRequest();//特编

xmlHttp.open("GET","ckuser.php?name="+username,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}


function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
//alert("来自服务器的响应:" + xmlHttp.responseText);
if(xmlHttp.responseText == "true"){
document.getElementById("ckuser").innerHTML = '此用户名以被人注册';
}
else if(xmlHttp.responseText == "false")
{
document.getElementById("ckuser").innerHTML = '检测通过';
}
}
}
}

[/php]

第三个文件就是php文件:ckuser.php
require_once("conn.php");
$username = $_GET["name"];
$query="select id from user where username='".$username."';";
$res=mysql_query($query);
if(mysql_num_rows($res)!=0)
{
echo "true";
}else
{
echo "false";
}


?>
最后一个是数据库链接文件conn.php

$conn=mysql_connect("localhost","root","l1314520") or die("数据库服务器连接错误".mysql_error());
mysql_select_db("test",$conn) or die("数据库访问错误".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/371914.htmlTechArticle实只要简单的实现ajax的检测用户名,正规点要分三个文件。我这里简单点: 第一个:index. php !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//E...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。