Home  >  Article  >  Backend Development  >  Sharing of ajax form verification methods in TP framework

Sharing of ajax form verification methods in TP framework

小云云
小云云Original
2018-02-06 10:05:511614browse

This article mainly introduces the operation method of dynamic form verification and the ajax form verification problem in the TP framework. Friends in need can refer to it. I hope it can help everyone.

Operation method of dynamic verification

function yz()
  {
    if(empty($_POST))
    {
      $this->display();
    }
    else
    {
      $db=D("Info");
       
      $shu=array(
        array("Code","require","代号不能为空",0,"regex",3),
      );
       
      if(!$db->validate($shu)->create())
      {
        echo $db->getError();
      }
      else
      {
        echo"验证通过";
      }
    }
  }

Use ajax in the framework to enter the code and display the name. To enter the template, use the above

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

form

Code name:

<input id="zhi" type="text" name="Code" />
<input type="submit" value="验证" 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("验证通过!");
             $("#xs").css("color","green");
           }
           else
           {
             $("#xs").html(data);
             $("#xs").css("color","red");
           }
         }
      });
    })
  });

Page display

Use ajax to display the form verification error message directly at the back

function yan()
  {
      $db=D("Info");
      $jieguo="";
      $shu=array(
        array("Code","require","代号不能为空",0,"regex",3),
      );
       
      if(!$db->validate($shu)->create())
      {
        $jieguo= $db->getError();
      }
      else
      {
        $jieguo="ok";
      }
      $this->ajaxReturn($jieguo,"eval");
  }

Page display

Related recommendations:

Using Vue.js in Laravel to implement Ajax form validation examples

php uses ajax Chinese garbled problem php ajax form verification php ajax paging code ajax and php basic teaching

yii2 modal pop-up window ActiveForm ajax Form validation_php example

The above is the detailed content of Sharing of ajax form verification methods in TP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn