在用户注册检测用户名是否存在我们要提供告诉用户你要注册的用户名是否可用,那么我们就得利用ajax技术来实例,下面是一款ajax php当用户输入完用户名时提示用户是否可用用的代码:
- <?php
- $title = isset($_get['title'])?$_get['title']:'';
- if( $title )
- {
- $sql ='select id from filecontent where title=''.$title.''';
- $q = mysql_query( $sql ) or die( mysql_error());
- if( mysql_num_rows( $q ) )
- {
- echo 1;
- }
- else
- {
- echo 0;
- }
- }
- else
- {
- echo 0;
- }
- ?>
-
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.111cn.net/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title> 用户注册检测用户名是否存在ajax + php代码</title>
- <script>
-
-
-
- function createxmlhttprequest(){
- if(window.activexobject){
- try {
- return new activexobject("microsoft.xmlhttp");
- } catch(e){
- return;
- }
- }else if(window.xmlhttprequest){
- try {
- return new xmlhttprequest();
- } catch(e){
- return;
- }
- }
- }
-
- function getrenews(value){
- var xmlhttp=createxmlhttprequest();
- var url = "t.php?action=check&title="+value+"&mt="+math.random(300000);
- if (value==""){
- return false ;
- }
- if (xmlhttp){
- callback = getreadystatehandler(xmlhttp);
- xmlhttp.onreadystatechange = callback;
- xmlhttp.open("get", url,true);
- xmlhttp.send(null);
- }
- }
-
-
- function getreadystatehandler(xmlhttp){
- return function (){
- if(xmlhttp.readystate == 4){
- if(xmlhttp.status == 200){
-
-
- if (xmlhttp.responsetext==1){
- document.getelementbyid("checkid").innerhtml="<font color='red'>对不起,你输入的用户名己被注册!</font>";
- }else{
- document.getelementbyid("checkid").innerhtml="可以注册";
- }
- }
- }
- }
- }
- </script>
- </head>
-
- <body>
- 给input框增加onblur事件,当用户输入完用户名就检测用户名,并给出提示。
- 输入用户名<input name="title" type="text" id="title" size="40" onblur="getrenews(this.value);"><span id="checkid"></span>
- </body>
- </html>
|