Home>

1 thinkphp framework to determine whether the input value is consistent with the value in the database

First you need to create a template named view.html under the view folder

<html>
<head>
  <script src="__ root __/public/js/jquery-1.11.2.min.js"></script>
</head>
<body>
    <div>Account:<input type="text" name="num"></div>
    <div></div>
</body>
</html>

I wrote a method zhuce () in the controller textcontroller.class.php to display the template

function zhuce ()
{
  $this->show ();
}

Here you need to use ajax to write, the first need to introduce the jquery package has been introduced above

<script type="text/javascript">
  $("#uid"). blur (function () {
  var num=$(this) .val ();
  $.ajax ({
    url:"__ controller __/chuli",    data:{num:num},    type:"post",    datatype:"text",    success:function (data)
     {
        if (data.trim () == "1")
         {
           $("#ts"). html ("This account already exists");
          }
       else
        {
             $("#ts"). html ("This account is available");
         }
      }
})
})
</script>

The above involves a Chuli method, so here we are going to start writing the Chuli method.

function chuli ()
{
   $n=d ("login");
   $num=$_ post ["num"];
   $aa=$n->where ("num =" {$num} "")->count ();
    $this->ajaxreturn ($aa, "eval");
}

In this way, you can determine whether this account is available.But we will find a small bug, if it is empty,Then it will show that this account is available,In order to avoid such mistakes,We can determine whether it is empty in js Ok)

2 Verification method:

The first is the method of determining if it is not empty.It is very simple to write directly in js here,Using the nation table, I wrote a jsdongtai.html file in the view folder,

<html>
<head>
  <script src="__ root __/public/js/jquery-1.11.2.min.js"></script>
</head>
<body>
  <div>
    Code:<input type="text" />
    <span></span>
  </div>
</body>
</html>

Then it is judged whether the code is empty

<script type="text/javascript">
  $("#code"). blur (function () {
   var code=$(this) .val ();
   if (code.trim () == "")
   {
     $("#ts) .html (" Code cannot be empty ");
   }
   else
  {
    $("#ts"). html ("Verified");
  }
})
</script>

Then use the jsdongtai method to adjust it. Function jsdongtai () {$this->show ();}

If we use ajax to adjust, it may be more troublesome,But it is much more convenient for other verifications,We can add multiple validations to the method

For example, we use a dongtai.html template and the html part of jsdongtai.html is the same,So we only need to write the ajax part.

<script type="text/javascript">
 $("#code"). blur (function () {
   var code=$(this) .val ();
   $.ajax ({
     url:"__ controller __/yanzheng",     data:{code:code}, //The first code must be the same as in the table
    type:"post",    datatype:"text",     success:function (data)
      {
        if (data.trim () == "1")
          {
            $("#ts"). html ("Verified"),          }
          else
          {
            $("#ts"). html ("cannot be empty here"),          }
      }
})
})
</script>

Here is the yanzheng method

function yanzheng ()
{
   $n=d ("nation");
   $a=array (
    array ("code", "require", "cannot be empty here"))
    if ($n->validate ($a)->create ())
     {
        $this->ajaxreturn ("1", "eval");
     }
     else
       {
         $this->ajaxreturn ($n->geterror (), "eval")
       }
}

When we usually do verification,Rarely use ajax, relatively troublesome,We can do it with simple js,For example, I do a verification on the mailbox,Add this sentence in jsdongtai.html

<div><input type="text" /><span></span></div>

Then we started to verify the mailbox,The js I use are all imported jquery packages, which have been introduced before.I won't go into details here

<script type="text/javascript">
$("#email"). blur (function () {
   var email=$(this) .val ();
   $zz =/^ ([a-za-z0-9 _-]) [email protected]([a-za-z0-9 _-]) + ((\. [a-za-z0-9 _-] {2,3 }) {1,2}) $/;
  if (email.match ($zz) == null)
  {
    $("#aa"). html ("The mailbox format is incorrect");
  }
  else
  {
     $("#aa"). html ("Mailbox verification succeeded");
  }
})
</script>
php
  • Previous Generate two-dimensional codes such as business cards and links based on JavaScript
  • Next Summary of thinkphp verification code