thinkphp Framework Forms Automatic Authentication login Registration AJAX automatic Authentication login Registration

Source: Internet
Author: User

Dynamic validation: (No model models needed)

1. Build a controller to do form operation (including validation)

<?phpnamespace Biaodan\controller; UseThink\controller;classBiaodancontrollerextendscontroller{ Public functionTest () {if(Empty($_post))//if $_post is empty, the Add page appears,        {            $this-Show (); }        Else    //if $_post is not empty, go to verify, verify success, add Database        {            $y= D ("Yonghu"); $arr=Array(//Create a validation rule                Array(' uid ', ' require ', ' username cannot be empty! ', 0),Array(' pwd ', ' pwd1 ', ' input passwords inconsistent ', 0, ' confirm '),//Equality Validation: Confirm (Verify that the two fields in the form are the same, the validation rule defined is a field name), meaning that pwd1 must be a field                Array(' email ', ' email ', ' email ' is incorrect ', 0),Array(' name ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID number incorrect ', 0, ' regex '),//Regular Expressions,                Array(' Ages ', ' 18,50 ', ' age not within range ', 0, ' between ')//Scope Validation            ); if($y->validate ($arr)->create ())//$y->validate ($arr), add a validation rule before executing            {                $y-Add (); }            Else            {                 die($y-GetError ()); }        }    }    }

2. HTML page corresponding to the Controller method:

<! DOCTYPE html Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

Ajax: (No model models needed)

1. Build a controller to do form operation (including validation)

<?phpnamespace Admin\controller; UseThink\controller;classZhucecontrollerextendsController { Public functionAdd () {$this-Show (); }         Public functionAddchuli () {$y= D ("Yonghubiao"); $arr=Array(        Array(' uid ', ' require ', ' username cannot be empty!! ‘),//Ps:require means that the field must be, or cannot be empty        Array(' pwd ', ' require ', ' two times password inconsistent! ‘),Array(' name ', ' require ', ' not filled in the name!! ‘),Array(' email ', ' email ', ' email ' format is not correct!! ‘),Array(' Age ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID card not legal ', 0, ' regex '),        ); if($y->validate ($arr),Create ()) {            $y-Add (); $this->ajaxreturn ("Registered success", "eval"); }Else{            $this->ajaxreturn ($y->geterror (), "eval"); }    }}

2. HTML page corresponding to the Controller method:

<! DOCTYPE html Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >    $("#dtn"). Click (function(){        varUID = $ ("#uid").Val (); varPWD = $ ("#pwd").Val (); varName = $ ("#name").Val (); varemail = $ ("#email").Val (); varAge = $ ("#age").Val (); $.Ajax ({URL: "__controller__/addchuli",Data: {uid:uid,pwd:pwd,name:name,email:email,age:age},DataType: "TEXT",type: "POST",Success:function(data) {alert (data);    }                    }); });</script>

thinkphp Framework Forms Automatic Authentication login Registration AJAX automatic Authentication login Registration

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.