• thinkphp 框架中的一部分方法解析


     1 thinkphp 框架 中判断输入的数值和数据库中的数值是否一致

       首先 需要在view文件夹下建一个模板 名为zhuce.html

    <html>
    <head>
        <script src="__ROOT__/Public/js/jquery-1.11.2.min.js"></script>
    </head>
    <body>
            <div> 账号:<input type="text" name="num" id="uid"></div>
            <div id="ts"></div>
    </body>
    </html>

    我在控制器TextController.class.php中写了一个方法zhuce(),显示模板

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

    这里需要用到ajax来写的,首选需要引入jquery包 已经在上面引入了

    <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("此账号已经存在");
                       } 
                  else    
                    {
                             $("#ts").html("此账号可用"); 
                     }
               }
    })
    })
    
    </script>

    上面涉及到一个chuli方法,那么这儿我们要开始写chuli方法了

    function chuli()
    {
          $n=D("login");
          $num=$_POST["num"];
          $aa=$n->where("num='{$num}'")->count();
           $this->ajaxReturn($aa,"eval");
    }

    这样就可以来判断这个账号是不是可以使用了,但是我们会发现有一个小bug,就是如果是空的话,那么会显示此账号可以使用,为了避免出现这样的失误,我们可以在js中 判断一下是不是为空  if(num.trim()==""){$("#ts").html("账号不可以为空")} else{执行ajax部分的内容就可以了}

    2 验证方法:

     首先是判断不为空的方法,这里可以直接用js来写是非常简单的,用的是nation表  我在view文件夹下写了一个jsdongtai.html的文件,

    <html>
    <head>
        <script src="__ROOT__/Public/js/jquery-1.11.2.min.js"></script>
    </head>
    <body>
       <div>
           代号:<input type="text" id="code"/>
           <span id="ts"></span>
       </div>
    </body>
    </html>

    然后就是判断代号是否为空

    <script type="text/javascript">
       $("#code").blur(function(){
         var code=$(this).val();
         if(code.trim()=="")
          {
             $("#ts).html("代号不能为空");
          }
         else
        {
           $("#ts").html("验证通过");
        }
    
    })
    
    </script>

    然后用jsdongtai方法调一下就可以了 function jsdongtai(){$this->show();}

    如果我们用ajax来调的话可能比较麻烦,但是对于其他的验证来说要方便的多了,我们可以在方法里面添加多个验证

    比如我们用一个dongtai.html的模板  和jsdongtai.html的html部分是一样的,这样我们就只需要写ajax部分就可以了   

    <script type="text/javascript">
      $("#code").blur(function(){
         var code=$(this).val();
         $.ajax({
             url:"__CONTROLLER__/yanzheng",
             data:{code:code},//第一个code必须是和表中的一致
            type:"POST",
            dataType:"TEXT",
             success:function(data)
               {
                    if(data.trim()=="1")
                       {
                           $("#ts").html("验证通过"),
                        }
                        else
                       {
                           $("#ts").html("此处不能为空"),
                        }
               }
    })
    })
    </script>

     下面是yanzheng方法

    function yanzheng()
    {
          $n=D("nation");
          $a=array(
           array("code","require","此处不能为空"))
           if($n->validate($a)->create())
             {
                   $this->ajaxReturn("1","eval");
              }
             else
                 {
                      $this->ajaxReturn($n->getError(),"eval")
                  }
    }

    在我们平时做验证的时候,很少会用ajax,相对来说比较麻烦,我们用简单的js就可以完成的,比如我做一个关于邮箱的验证,在jsdongtai.html中加入这一句

    <div><input type="text" id="email"/><span id="aa"></span></div>

    然后我们开始做邮箱的验证了,我用的js都是引入的jquery包,前面已经引入过了,这里就不再详说了

    <script type="text/javascript">
    
    $("#email").blur(function(){
         var email = $(this).val();
         $zz=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        if(email.match($zz)==null)
       {
            $("#aa").html("邮箱格式不正确");        
       }
       else
       {
             $("#aa").html("邮箱验证成功");    
       }
    })
    </script>
  • 相关阅读:
    Java调用R环境配置问题:Cannot find JRI native library!
    在odoo 10.0配置文件中修改ip地址
    WordPress国外开源主题Enigma h1、h2、h3……标题不显示的解决办法
    ubuntu下编译为知笔记
    ubuntu linux 使用什么下载工具
    通过修改qt设置,解决LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”
    [DONE]ReferenceError: WebSocket is not defined pomelo
    [DONE]pomelo npm-install 出现 AttributeError: 'module' object has no attribute 'script_main'
    finished running <my app>” on the targeted device
    c++中64位int与字符串的转换
  • 原文地址:https://www.cnblogs.com/xiaodouding/p/6812867.html
Copyright © 2020-2023  润新知