Xajax FORM example

Source: Internet
Author: User
Tags valid email address

Copy codeThe Code is as follows:
<? Php
// Signup. php
// Demonstrates a simple multipage form using xajax
// And the xajax. getFormValues () function.
// Using xajax version 0.1 beta4
// Http://xajax.sourceforge.net
Session_start ();
Include ("xajax. inc. php ");
Function processForm ($ aFormValues)
{
If (array_key_exists ("username", $ aFormValues ))
{
Return processAccountData ($ aFormValues );
}
Else if (array_key_exists ("firstName", $ aFormValues ))
{
Return processPersonalData ($ aFormValues );
}
}
Function processAccountData ($ aFormValues)
{
$ ObjResponse = new xajaxResponse ();
$ BError = false;
If (trim ($ aFormValues ['username']) = "")
{
$ ObjResponse-> addAlert ("Please enter a username .");
$ BError = true;
}
If (trim ($ aFormValues ['newpass1']) = "")
{
$ ObjResponse-> addAlert ("You may not have a blank password .");
$ BError = true;
}
If ($ aFormValues ['newpass1']! = $ AFormValues ['newpasss2'])
{
$ ObjResponse-> addAlert ("Passwords do not match. Try again .");
$ BError = true;
}
If (! $ BError)
{
$ _ SESSION = array ();
$ _ SESSION ['newaccount'] ['username'] = trim ($ aFormValues ['username']);
$ _ SESSION ['newaccount'] ['Password'] = trim ($ aFormValues ['newpass1']);
$ SForm = "<form id = \" signupForm \ "action = \" javascript: void (null); \ "onsubmit = \" submitSignup (); \ "> ";
$ SForm. = "<div> First Name: </div> <input type = \" text \ "name = \" firstName \ "/> </div> ";
$ SForm. = "<div> Last Name: </div> <input type = \" text \ "name = \" lastName \ "/> </div> ";
$ SForm. = "<div> Email: </div> <input type = \" text \ "name = \" email \ "/> </div> ";
$ SForm. = "<div class = \" submitDiv \ "> <input id = \" submitButton \ "type = \" submit \ "value = \" done \ "/> </div> ";
$ SForm. = "</form> ";
$ ObjResponse-> addAssign ("formDiv", "innerHTML", $ sForm );
$ ObjResponse-> addAssign ("formWrapper", "style. backgroundColor", "rgb (67,149, 97 )");
$ ObjResponse-> addAssign ("outputDiv", "innerHTML", "\$ _ SESSION: <pre> ". var_export ($ _ SESSION, true ). "</pre> ");
}
Else
{
$ ObjResponse-> addAssign ("submitButton", "value", "continue-> ");
$ ObjResponse-> addAssign ("submitButton", "disabled", false );
}
Return $ objResponse-> getXML ();
}
Function processPersonalData ($ aFormValues)
{
$ ObjResponse = new xajaxResponse ();
$ BError = false;
If (trim ($ aFormValues ['firstname']) = "")
{
$ ObjResponse-> addAlert ("Please enter your first name .");
$ BError = true;
}
If (trim ($ aFormValues ['lastname']) = "")
{
$ ObjResponse-> addAlert ("Please enter your last name .");
$ BError = true;
}
If (! Eregi ("^ [a-zA-Z0-9] + [_ a-zA-Z0-9-] * (\. [_ a-z0-9-] +) * @ [a-z ?????? 0-9] + (-[a-z ?????? 0-9] +) * (\. [a-z ?????? 0-] +) * (\. [a-z] {2, 4}) $ ", $ aFormValues ['email '])
{
$ ObjResponse-> addAlert ("Please enter a valid email address .");
$ BError = true;
}
If (! $ BError)
{
$ _ SESSION ['newaccount'] ['firstname'] = $ aFormValues ['firstname'];
$ _ SESSION ['newaccount'] ['lastname'] = $ aFormValues ['lastname'];
$ _ SESSION ['newaccount'] ['email '] = $ aFormValues ['email'];
$ ObjResponse-> addAssign ("formDiv", "style. textAlign", "center ");
$ SForm = "Account created. <br/> Thank you .";
$ ObjResponse-> addAssign ("formDiv", "innerHTML", $ sForm );
$ ObjResponse-> addAssign ("formWrapper", "style. backgroundColor", "rgb (149 )");
$ ObjResponse-> addAssign ("outputDiv", "innerHTML", "\$ _ SESSION: <pre> ". var_export ($ _ SESSION, true ). "</pre> ");
}
Else
{
$ ObjResponse-> addAssign ("submitButton", "value", "done ");
$ ObjResponse-> addAssign ("submitButton", "disabled", false );
}
Return $ objResponse-> getXML ();
}
$ Xajax = new xajax ();
// $ Xajax-> debugOn ();
$ Xajax-> registerFunction ("processForm ");
$ 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 ();?>
<Style type = "text/css">
# FormWrapper {
Color: rgb (255,255,255 );
Background-color: rgb (149,67, 97 );
Width: 200px;
}
# Title {
Text-align: center;
Background-color: rgb (0, 0 );
}
# FormDiv {
Padding: 25px;
}
. SubmitDiv {
Margin-top: 10px;
Text-align: center;
}
</Style>
<Script type = "text/javascript">
Function submitSignup ()
{
Xajax. $ ('submitclick'). disabled = true;
Xajax. $ ('submitclick'). value = "please wait ...";
Xajax_processForm (xajax. getFormValues ("signupForm "));
Return false;
}
</Script>
</Head>
<Body>
<Div id = "formWrapper">
<Div id = "title"> Create a New Account </div>
<Div id = "formDiv">
<Form id = "signupForm" action = "javascript: void (null);" onsubmit = "submitSignup ();">
<Div> Username: </div> <input type = "text" name = "username"/> </div>
<Div> Password: </div> <input type = "password" name = "newPass1"/> </div>
<Div> Confirm Password: </div> <input type = "password" name = "newPass2"/> </div>
<Div class = "submitDiv"> <input id = "submitButton" type = "submit" value = "continue->"/> </div>
</Form>
</Div>
</Div>
<Div id = "outputDiv">
</Div>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.