- 论坛徽章:
- 0
|
xajax类:http://xajax.sourceforge.net
昨晚开始接触xajax,感觉很不错,于是开始写了一些代码,希望能对象我这样刚学的新手有用,高手就别看了。。呵呵。。。
- <?php
- require('xajax.inc.php');
- $xajax = new xajax;
- $xajax->registerFunction("check");
- function check($data)
- {
- $response = new xajaxResponse();
- if (strlen($data['username'])<3)
- {
- $response->addAssign("info","innerHTML","用户名长度太短");
- return $response->getxml();
- }
- else if(strlen(($data['username']))>20)
- {
- $response->addAssign("info","innerHTML","用户名长度太长");
- return $response->getxml();
- }
- else
- {
- $response->addAssign("info","innerHTML","用户名符合规格");
- return $response->getxml();
- }
- }
- $xajax->processRequests();
- $xajax->printJavascript();
- ?>
- <script language="javascript">
- function chkform()
- {
- xajax_check(xajax.getFormValues('form1'));
- }
- </script>
- <form id="form1" name="form1" action="?" method="post">
- username:<input type="text" id="username" name="username"><div id="info" name="info"></div>
- password:<input type="text" id="password" name="password" onclick="chkform();">
- </form>
复制代码
检查用户名是否存在
- <?php
- require('xajax.inc.php');
- $xajax = new xajax;
- function userisexist($username)
- {
- $conn=@mysql_connect('localhost','root','root');
- if (!$conn){die(mysql_errorno());}
- $selectdb=@mysql_select_db('phpwind');
- if (!$selectdb){die(mysql_errorno());}
- $sql="select username from pw_members where username='".$username."'";
- $result=mysql_query($sql);
- $rs=mysql_num_rows($result);
- if ($rs==true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- function checkuser($username)
- {
- $response = new xajaxResponse();
- if (userisexist($username)==true)
- {
- $response->addAssign("showmsg","innerHTML","对不起,".$username."已经存在");
- return $response->getxml();
- }
- else
- {
- $response->addAssign("showmsg","innerHTML","恭喜,".$username."可以注册");
- return $response->getxml();
- }
- }
- $xajax->registerFunction('checkuser');
- $xajax->processRequests();
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <?php $xajax->printJavascript(); ?>
- <script language="javascript">
- function checkform()
- {
- xajax_checkuser(document.getElementById('username').value);
- }
- </script>
- </head>
- <body>
- <form action="?" id="form1" name="form1">
- <input type="input" id="username" name="username"><div id="showmsg" name="showmsg"></div>
- <input type="button" id="checkbt" name="checkbt" onclick="checkform();" value="Check Now">
- </form>
- </body>
- </html>
复制代码
使用xajax检测帐号是否可以注册。
使用的是 xajax 0.2
check.php
- <?php
- require('reg.common.php');
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <?php $xajax->printJavascript(); ?>
- <script language="javascript">
- function checkform()
- {
- xajax_checkreg(document.getElementById('username').value);
- }
- </script>
- </head>
- <body>
- <form action="?" id="form1" name="form1">
- <input type="input" id="username" name="username"><div id="showmsg" name="showmsg"></div>
- <input type="button" id="checkbt" name="checkbt" onclick="checkform();" value="Check Now">
- </form>
- </body>
- </html>
复制代码
reg.common.php
- <?php
- define ('XAJAX_DEFAULT_CHAR_ENCODING','gb2312');
- require('xajax.inc.php');
- $xajax = new xajax('reg.server.php');
- $xajax->registerFunction("checkreg");
- ?>
复制代码
reg.server.php
- <?php
- require_once('reg.common.php');
- function userisexist($username)
- {
- $conn=@mysql_connect('localhost','root','root');
- if (!$conn){die(mysql_errorno());}
- $selectdb=@mysql_select_db('phpwind');
- if (!$selectdb){die(mysql_errorno());}
- $sql="select username from pw_members where username='".$username."'";
- $result=mysql_query($sql);
- $rs=mysql_num_rows($result);
- if ($rs==true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- function isvalidname($username)
- {
- $badkey=array("\\",'|','??',' ',"'",'"','/','*',',','~',';','<','>','$',"\r","\t","\n");
- foreach($badkey as $value)
- {
- if (strpos($username,$value)!==false)
- {
- return false;
- exit;
- }
- }
- return true;
- }
- function checkreg($username)
- {
- $response = new xajaxResponse();
- if (strlen($username)<3||strlen($username)>20)
- {
- $response->addClear('showmsg',"innerHTML");
- $response->addAlert('对不起,用户名太长或者太短!');
- }
- elseif(isvalidname($username)==false)
- {
- $response->addClear('showmsg',"innerHTML");
- $response->addAlert('对不起,用户名含有非法字符!');
- }
- else
- {
- if(userisexist($username)==true)
- {
- $response->addAssign('showmsg',"innerHTML","对不起, ".$username." 已经存在!");
- }
- else
- {
- $response->addAssign('showmsg',"innerHTML","恭喜, ".$username." 可以使用!");
- }
- }
-
- return $response;
- }
- $xajax->processRequests();
- ?>
复制代码
[ 本帖最后由 郁闷小子 于 2005-12-20 19:24 编辑 ] |
|