在用戶注冊(cè)檢測(cè)用戶名是否存在我們要提供告訴用戶你要注冊(cè)的用戶名是否可用,那么我們就得利用ajax技術(shù)來實(shí)例,下面是一款ajax php當(dāng)用戶輸入完用戶名時(shí)提示用戶是否可用用的代碼:
- <?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;//開源代碼Vevb.com
- }
- ?>
- <!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> 用戶注冊(cè)檢測(cè)用戶名是否存在ajax + php代碼</title>
- <script>
- // 用戶注冊(cè)檢測(cè)用戶名是否存在ajax + php代碼
- function createxmlhttprequest(){//創(chuàng)建xmlhttprequest對(duì)象
- if(window.activexobject){//ie
- try {
- return new activexobject("microsoft.xmlhttp");
- } catch(e){
- return;
- }
- }else if(window.xmlhttprequest){//mozilla,firefox
- try {
- return new xmlhttprequest();
- } catch(e){
- return;
- }
- }
- }
- function getrenews(value){//主調(diào)函數(shù)
- 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);
- }
- }
- //返回0代表用戶名可用,否則提示己被注冊(cè)。
- function getreadystatehandler(xmlhttp){//服務(wù)器返回后處理函數(shù)
- return function (){
- if(xmlhttp.readystate == 4){
- if(xmlhttp.status == 200){
- if (xmlhttp.responsetext==1){
- document.getelementbyid("checkid").innerhtml="<font color='red'>對(duì)不起,你輸入的用戶名己被注冊(cè)!</font>";
- }else{
- document.getelementbyid("checkid").innerhtml="可以注冊(cè)";
- }
- }
- }
- }
- }
- </script>
- </head>
- <body>
- 給input框增加onblur事件,當(dāng)用戶輸入完用戶名就檢測(cè)用戶名,并給出提示。
- 輸入用戶名<input name="title" type="text" id="title" size="40" onblur="getrenews(this.value);"><span id="checkid"></span>
- </body>
- </html>
新聞熱點(diǎn)
疑難解答