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!