PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ThinkPhp教程 > 正文

    ThinkPHP3.2使用AJAX返回数据

    作者:w634381967来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:前台首先必须引入jquery.form.js,没有的自己百度下载JS代码复制代码 代码如下:<script type="text/javascript">$(function(){$(&#39;#fo...
    前台

    首先必须引入jquery.form.js,没有的自己百度下载

    JS代码


     
    复制代码 代码如下:
    <script type="text/javascript">
    $(function(){
    $('#form1').ajaxForm({
    beforeSubmit: checkForm, // 此方法主要是提交前执行的方法,根据需要设置
    success: complete, // 这是提交后的方法
    dataType: 'json'
    });
    function checkForm(){
    if( '' == $.trim($('#title').val())){
    $('#result').html('标题不能为空').show();
    return false;
    }
    //可以在此添加其它判断
    }
    function complete(data){
    if (data.status==1){
    $('#result').html(data.info).show();
    alert(data.id);
    var html = '<div class="result" style=\'font-weight:normal;background:#A6FF4D\'><div style="border-bottom:1px dotted silver">标题:'+data.id+' [ '+data.logintime+' ]</div><div class="content">内容:'+data.loginip+'</div></div>';
    $('#list').prepend(html);
    }else{
    alert(2);
    $('#result').html(data.info).show();
    }
    }

    });

    </script>

    HTML代码

     
    复制代码 代码如下:
    <form id="form1" method="post" action="/login.php">

    <input type="text" name="username" />

    <input type="password" name="password" />

    <input type="submit"   />

    <form>

    当时点击表单的时候他就使用ajax提交了

    PHP代码


     
    复制代码 代码如下:
    //用户登陆处理表单
    public function login(){
    //if(!IS_POST) $this->error("非法操作!");
    $db = M('user');
    $username =I('username');
    $password =I('password','','md5');
    $user = $db->where(array('username'=>$username,'password'=>$password,))->find();
    if (!$user) {
    $this->error('账号或密码错误');
    }
    if($user['lock'] == '1'){
    $this->error('用户被锁定,请联系管理员解锁');
    }

    //更新数据信息
    $data =array(
    'id' => $user['id'],
    'logintime' => time(),
    'loginip' => get_client_ip(),
    );

    $db->save($data);
    //$this->success("登陆成功");
    $vo =$data;
    $vo['status'] = 1; //登陆成功
    $this->ajaxReturn($vo,"json");
    //把用登陆信息存到SESSION
    session("id",$user['id']);
    session("username",$user['username']);
    session("logintime",date('Y-m-d H:i:s'),$user['logintime']);
    session("loginip",$user['loginip']);
    redirect(__APP__);

    }
     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-83-1099-1.html
    相关热词搜索: