• jq和thinkphp经常使用的几种ajax


    第一种方法

     

    bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

    第二种方法

    jquery方法:
    
    MessageAction.class.php
    
    <?php
    class MessageAction extends Action{
       
        function index(){
            $this->display();   
        }
       
        function add(){
            //ajaxReturn(数据,‘提示信息‘,状态)   
            $m=M(‘message‘);
            if($m->add($_GET)){
                $this->ajaxReturn($_GET,‘添加信息成功‘,1);
            }else{
                $this->ajaxReturn(0,‘添加信息失败‘,0);   
            }
        }
     
    }
    ?>
    
    模板index.html
    
    <html>
    <head>
    <script type="text/javascript" src="__PUBLIC__/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $(‘input:button‘).click(function(){
                var $title=$(‘input[name="title"]‘).val();
                var $message=$(‘input[name="message"]‘).val();
                $mess=$(‘#mess‘);
               $.getJSON(‘__URL__/add‘,{title:$title,message:$message},function(json){
                    //alert(json);return false;
                    if(json.status==1){
                        $mess.slideDown(3000,function(){
                            $mess.css(‘display‘,‘block‘);   
                        }).html(‘标题为‘+json.data.title+‘信息为‘+json.data.message);   
                    }else{
                        $mess.slideDown(3000,function(){
                            $mess.css(‘display‘,‘block‘);   
                        }).html(‘信息添加失败,请检查‘);   
                    }       
                });
            })   
        })
    </script>
    </head>
    <body>
    <div style="display:none; color:red;" id="mess"></div>
    <form action="" method="get">
      标题:<input type="text" name="title" /><br />
      信息:<input type="text" name="message" /><br />
           <input type="button" value="提交" />
    </form>
    </body>
    </html>
    
    ThinkPHP方法:
    
    MessageAction.class.php
    
    <?php
    class MessageAction extends Action{
       
        function index(){
            $this->display();   
        }
    
        function addtwo(){
            $m=M(‘message‘);
            if($vo=$m->create()){
                if($m->add()){
                    $this->ajaxReturn($vo,‘添加成功‘,1);   
                }else{
                    $this->ajaxReturn(0,‘添加失败‘,0);   
                }   
            }else{
                $this->error($m->getError());   
            }
                  
        }
     
    }
    ?>
    
    模板index.html
    
    <html>
    <head>
    <script type="text/javascript" src="__PUBLIC__/Js/Base.js"></script>
    <script type="text/javascript" src="__PUBLIC__/Js/prototype.js"></script>
    <script type="text/javascript" src="__PUBLIC__/Js/mootools.js"></script>
    <script type="text/javascript" src="__PUBLIC__/Js/ThinkAjax.js"></script>
    <script type="text/javascript">
        function add(){
            //ThinkAjax.sendForm(表单ID,URL,回调函数,信息显示的地方);
            ThinkAjax.sendForm(‘frm‘,‘__URL__/addtwo‘,wc);   
        }
        function wc(data,status){
            if(status!=1){
                alert(‘发送失败‘);
            }else{
                $(‘list‘).innerHTML+=‘标题‘+data.title+‘,信息‘+data.message;   
            }   
        }
    </script>
    
    </head>
    <body>
  • 相关阅读:
    Zookeeper 系列(五)Curator API
    Zookeeper 系列(四)ZKClient API
    Zookeeper 系列(三)Zookeeper API
    Zookeeper 系列(二)安装配制
    [bzoj 2393] Cirno的完美算数教室 (容斥原理+dfs剪枝)
    [Sdoi2013] [bzoj 3198] spring (hash+容斥原理)
    [bzoj 1471] 不相交路径 (容斥原理)
    [bzoj 3701] Olympic Games (莫比乌斯反演)
    [bzoj 2693] jzptab & [bzoj 2154] Crash的数字表格 (莫比乌斯反演)
    [51Nod 1244]
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4657909.html
Copyright © 2020-2023  润新知