欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xajax的FORM例子

程序员文章站 2022-06-24 16:06:17
复制代码 代码如下:
复制代码 代码如下:

<?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['newpass2'])
        {
                $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><div><input type=\"text\" name=\"firstname\" /></div>";
                $sform .="<div>last name:</div><div><input type=\"text\" name=\"lastname\" /></div>";
                $sform .="<div>email:</div><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-9-]+)*(\.[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(67,97,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,0);
                }
                #formdiv{
                        padding: 25px;
                }
                .submitdiv{
                        margin-top: 10px;
                        text-align: center;
                }
                </style>
                <script type="text/javascript">
                function submitsignup()
                {
                        xajax.$('submitbutton').disabled=true;
                        xajax.$('submitbutton').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><div><input type="text" name="username" /></div>
                                        <div>password:</div><div><input type="password" name="newpass1" /></div>
                                        <div>confirm password:</div><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>

上一篇: redux与react-redux

下一篇: MySQL锁机制