ThinkPHP 修改状态

    {if($val['b_state']==1)}
    已上架
    {else}
    已下架
    {/if}
绑定点击事件
$('.state').click(function (){
    var b_id = $(this).attr('b_id');
    var b_state = $(this).attr('b_state');
    var that = $(this);
    $.ajax({
        url:'state',
        data:{
            b_id:b_id,
            b_state:b_state,
        },
        type:'get',
        success:function (res){
            if(res.code ==200){
                if(b_state==1){
                    that.html('已下架');
                    that.attr('b_state',0);
                }else{
                    that.html('已上架');
                    that.attr('b_state',1);
                }

            }
        }
    })
})

控制器

public function state()
{
    $b_state = request()->get('b_state');
    $b_id = request()->get('b_id');
    //dd($b_state);
    //实例化模型层
    $model = new \app\model\showList();
    //想模型层传递ID的值
    $data = $model->state($b_state,$b_id);
    return json(['code'=>'200','msg'=>'修改成功']);
}

模型层

public function state($b_state,$b_id)
{
    if($b_state == 1){
        return $this->where('b_id',$b_id)
                    ->save(['b_state'=>0]);
    }else{
        return $this->where('b_id',$b_id)
                    ->save(['b_state'=>1]);
    }
}

你可能感兴趣的:(thinkphp,javascript)