Please enter text in the text box. PHP ajax framework xajax introduction and trial
1. Comparison of xajax and other ajax frameworks
The xajax function is very simple, but very flexible! ~It is not like some other large frameworks. Its functions are indeed powerful, but its execution speed cannot be complimented. . Although it has many functions, it is not flexible enough. There are so many APIs that learning it is like learning a new language.
2. Introduction to xajax functions
The function of xajax is relatively simple, but because of its simplicity, it is flexible. At the same time, this also requires users to have a certain understanding of javascrīpt/vbs client scripts. Because its function is relatively lively. It can be said that nothing can be done by simply using xajax, but everything can be done with js/vbs.
Xajax mainly uses the xajaxResponse class, which provides some methods, for example:
1. addAlert($sMsg)
pop up warning
2. addsscript($sJS)
Execute a certain piece of js
3. $objResponse->addAssign("","","")
Attach a value to an element on the page, or modify its properties, etc. . . .
So xajax is not dead, it cannot make XXX XXX functions, but it can flexibly control the js/vbs of the client to achieve the effect we want to achieve.
3. Xajax installation and configuration does not require special installation and configuration. Just download its file package and unzip it to the website directory. Download address:
http://www.xajaxproject.org/
3. Use xajax for membership registration and login
1. The database uses mysql5.0, the database name is zl, the table name is zl_user table structure
id int(11) auto_increment
zl_user varchar(50)
zl_pwd varchar(50)
email varchar(50)
2. reg.php registration file (with instructions)
<?php
require_once("inc/xajax.inc.php");
//To use xajax, you must first introduce xajax.inc.php
$xajax = new xajax("inc/signup.php");
//Create a xajax object for singup.php
$xajax->registerFunction("processForm");
//Use the processForm function in singup.php
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href=" http://blogbeta.blueidea.com/css/style.css " _fcksavedurl="" http://blogbeta.blueidea.com/css/style.css "" type=" text/css">
<title>Untitled Document</title>
<?php $xajax->printJavascrīpt('inc/'); ?>
<scrīpt type="text/javascrīpt">
function submitSignup()
{
xajax.$('submitButton').disabled=true;
xajax.$('submitButton').value=" http://blogbeta.blueidea.com/wait ...";
//Modify the attribute with ID submitButton
xajax_processForm(xajax.getFormValues("signupForm"));
//Here xajax_ is followed by which function to use. Here is processForm, followed by a collection of signupForm form items.
return false;
}
</scrīpt>
</head>
<body><form id="signupForm" action="javascrīpt:void(null);" ōnSubmit="submitSignup();">
<div id="main">
<div id="m1">User registration</div>
<div id="formDiv">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="31%" align="right">Username:</td>
<td width="69%"><input name="usr" type="text" id="usr" />
*</td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="pwd" type="password" id="pwd" />
*</td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="pwd2" type="password" id="pwd2" />
*</td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input name="email" type="text" id="email" />
* Can be used to retrieve password</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submitButton" value="Submit" class="button" />
<input type="reset" name="Submit2" value="Reset" class="button" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
After clicking submit, execute processForm function 3 in singup.php
, inc/singup.php
<?php
define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gb2312' );
//Note here, gb2312 must be set, otherwise the Chinese will be garbled
require_once("xajax.inc.php");
require_once("function.php");
$xajax = new xajax();
$xajax->registerFunction("processForm");
//Same as reg.php file
function processForm($aFormValues)
{
$objResponse = new xajaxResponse();
require_once("conn.php");
$usr=$aFormValues['usr'];
$email=$aFormValues['email'];
$pwd=$aFormValues['pwd'];
$pw=Md5($pwd);
$errmsg="";
//Illegal characters to be filtered
$ArrFiltrate=array("'",";","union");
foreach($aFormValues as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
$objResponse->addAlert("The entered information contains illegal characters"' ; union!"");
$objResponse->addAssign("submitButton","value","continue");
$objResponse->addAssign("submitButton","disabled",false);
return $objResponse;
}
}
if (trim($usr) == "")
{
$errmsg.="Please enter username!n";
}
if (trim($pwd) == "")
{
$errmsg.="Please enter password!n";
}
if ($pwd != $aFormValues['pwd2'])
{
$errmsg.="The passwords entered twice are inconsistent!n";
}
if (!CheckEmailAddr($email))
{
$errmsg.="Email address is incorrect!n";
}
$sql="select * from zl_usr where zl_usr='$usr'";
$result=mysql_query($sql,$db);
if($myrow=mysql_fetch_array($result)){
$errmsg.="Username already exists!n";
}
if ($errmsg=="")
{
$sForm = "Registration successful<br>Username:".$usr."<br>email:".$email."";
$sql="insert into zl_usr(zl_usr,zl_pwd,email) values('$usr','$pw','$email')";
$result=mysql_query($sql,$db);
$objResponse->addAssign("formDiv","innerHTML",$sForm);
}
else
{
$objResponse->addAlert($errmsg);
//Pop up error message
$objResponse->addAssign("submitButton","value","continue");
//Modify the value of submitButton to continue
$objResponse->addAssign("submitButton","disabled",false);
//Modify the properties of the submitButton button
}
return $objResponse;
}
$xajax->processRequests();
?>
The validity of the information is judged in this file, including: whether the user name has been registered, whether there are illegal characters in the information, whether the email address is correct, whether the passwords entered twice are consistent, and if there are no errors, they are entered into the database. ,and
$objResponse->addAssign("formDiv","innerHTML",$sForm);
Re-insert the code in formDiv with the content $sForm
$sForm = "Registration successful<br>Username:".$usr."<br>email:".$email."";
If there is an error message then
$objResponse->addAlert($errmsg);
//Pop up error message
$objResponse->addAssign("submitButton","value","continue");
$objResponse->addAssign("submitButton","disabled",false);
//Modify the attributes of submitButton
3. login.php login file
<?php
require_once("inc/xajax.inc.php");
$xajax = new xajax("inc/login.php");
$xajax->registerFunction("processForm");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href=" http://blogbeta.blueidea.com/css/style.css " _fcksavedurl="" http://blogbeta.blueidea.com/css/style.css "" type=" text/css">
<title>Untitled Document</title>
<?php $xajax->printJavascrīpt('inc/'); ?>
<scrīpt type="text/javascrīpt">
function submitSignup()
{
xajax.$('submitButton').disabled=true;
xajax.$('submitButton').value=" http://blogbeta.blueidea.com/wait ...";
xajax_processForm(xajax.getFormValues("signupForm"));
return false;
}
</scrīpt>
</head>
<body><form id="signupForm" action="javascrīpt:void(null);" ōnSubmit="submitSignup();">
<div id="main">
<div id="m1">User login</div>
<div id="formDiv">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="31%" align="right">Username:</td>
<td width="69%"><input name="usr" type="text" id="usr" />
*</td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="pwd" type="password" id="pwd" />
*</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submitButton" value="Submit" class="button" />
<input type="reset" name="Submit2" value="Reset" class="button" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
4. inc/login.php login processing file
<?php
define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gb2312' );
require_once("xajax.inc.php");
require_once("function.php");
$xajax = new xajax();
$xajax->registerFunction("processForm");
function processForm($aFormValues)
{
$objResponse = new xajaxResponse();
require_once("conn.php");
$usr=$aFormValues['usr'];
$email=$aFormValues['email'];
$pwd=$aFormValues['pwd'];
$pw=Md5($pwd);
$errmsg="";
//Illegal characters to be filtered
$ArrFiltrate=array("'",";","union");
foreach($aFormValues as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
$objResponse->addAlert("The entered information contains illegal characters"' ; union!"");
$objResponse->addAssign("submitButton","value","continue");
$objResponse->addAssign("submitButton","disabled",false);
return $objResponse;
}
}
if (trim($usr) == "")
{
$errmsg.="Please enter your username!n";
}
if (trim($pwd) == "")
{
$errmsg.="Please enter your password!n";
}
$sql="select * from zl_usr where zl_usr='$usr' and zl_pwd='$pw'";
$result=mysql_query($sql,$db);
if(!$myrow=mysql_fetch_array($result)){
$errmsg.="Username does not exist, or password is wrong!n";
}
if ($errmsg=="")
{
$sForm = "Login successful";
$objResponse->addAssign("formDiv","innerHTML",$sForm);
}
else
{
$objResponse->addAlert($errmsg);
$objResponse->addAssign("submitButton","value","continue");
$objResponse->addAssign("submitButton","disabled",false);
}
return $objResponse;
}
$xajax->processRequests();
?>
The principles of login and registration are similar, so no more nonsense:)
In addition, the following are the two file codes used conn.php function.php
conn.php
<?php
$database="zl";//MYSQL database name
$db = mysql_connect("127.0.0.1", "root","123456");//MYSQL database user name and password
mysql_select_db($database,$db);
?>
function.php
<?php
function CheckEmailAddr($C_mailaddr)
{
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+ )*$",
$C_mailaddr))
{
return false;
}
return true;
}
//Whether there is a value in the array
function FunStringExist($StrFiltrate,$ArrFiltrate){
foreach ($ArrFiltrate as $key=>$value){
if (eregi($value,$StrFiltrate)){
return true;
}
}
return false;
}
?>