The operation method of dynamic form verification and ajax Form Verification in the TP framework, tpajax

Source: Internet
Author: User

The operation method of dynamic form verification and ajax Form Verification in the TP framework, tpajax

Dynamic verification procedure

Function yz () {if (empty ($ _ POST) {$ this-> display ();} else {$ db = D ("Info "); $ shu = array ("Code", "require", "Code cannot be blank", 0, "regex", 3),); if (! $ Db-> validate ($ shu)-> create () {echo $ db-> getError () ;}else {echo "verified ";}}}

In the Framework, ajax is used to display the name of the input code to the template.

function ming()  {    $code=$_POST["code"];    $db=D("Info");    $data=$db->find($code);    $name=$data["name"];    $this->ajaxReturn($name,"eval");  }

  Form

Code:

<Input id = "zhi" type = "text" name = "Code"/> <input type = "submit" value = "verify" id = "en"/>

Js Code

<Script type = "text/javascript"> $ (document ). ready (function (e) {$ ("# en "). click (function () {var code = $ ("# zhi "). val (); $. ajax ({url: "_ CONTROLLER _/ming", data: {code: code}, type: "POST", dataType: "TEXT", success: function (data) {alert (data) ;}}) ;}$ ("# yz "). blur (function () {var code = $ (this ). val (); $. ajax ({url: "_ CONTROLLER _/yan", data: {Code: code}, type: "POST", dataType: "TEXT", success: function (data ) {If (data. trim () = "OK") {$ ("# xs" ).html ("verified! "); $ (" # Xs ").css (" color "," green ");} else {$ (" # xs ").html (data ); $ ("# xs" ).css ("color", "red ");}}});})});

Page display

Use ajax to Display error messages for form verification directly at the end

Function yan () {$ db = D ("Info"); $ jieguo = ""; $ shu = array ("Code", "require ", "Code cannot be blank", 0, "regex", 3),); if (! $ Db-> validate ($ shu)-> create () {$ jieguo = $ db-> getError ();} else {$ jieguo = "OK ";} $ this-> ajaxReturn ($ jieguo, "eval ");}

Page display

Summary

The above section describes the dynamic form verification method and ajax Form Verification in the TP framework. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.