菜单保存save函数 public function save(){ $mid = (int)input('post.mid'); $data['pid'] = (int)input('post.pid'); $data['title'] = trim(input('post.title')); $data['controller'] = trim(input('post.controller')); $data['method'] = trim(input('post.method')); $data['ord'] = (int)input('post.ord'); $data['ishidden'] = (int)input('post.ishidden'); $data['status'] = (int)input('post.status'); if($data['title'] == ''){ exit(json_encode(array('code'=>1,'msg'=>'菜单名称不能为空'))); } if($data['pid']>0 && $data['controller'] == ''){ exit(json_encode(array('code'=>1,'msg'=>'控制器名称不能为空'))); } if($data['pid']>0 && $data['method'] == ''){ exit(json_encode(array('code'=>1,'msg'=>'方法名称不能为空'))); } if($mid){ $res = $this->db->table('admins_menu')->where(array('mid'=>$mid))->update($data); }else{ $res = $this->db->table('admins_menu')->insert($data); } if(!$res){ exit(json_encode(array('code'=>1,'msg'=>'保存失败'))); } exit(json_encode(array('code'=>0,'msg'=>'保存成功'))); }
前台页面点击调用javascript的save函数
<script type="text/javascript"> layui.use(['layer','form'],function(){ var form = layui.form; layer = layui.layer; $ = layui.jquery; }); function save(){ var pid = parseInt($('input[name="pid"]').val()); var title = $.trim($('input[name="title"]').val()); var controller = $.trim($('input[name="controller"]').val()); var method = $.trim($('input[name="method"]').val()); if(title==''){ layer.alert('请输入菜单名称',{'icon':2}); return; } if(pid>0 && controller==''){ layer.alert('请输入控制器',{'icon':2}); return; } if(pid>0 && method==''){ layer.alert('请输入方法名称',{'icon':2}); return; } $.post('/index.php/admins/menu/save',$('form').serialize(),function(res){ if(res.code>0){ layer.alert(res.msg,{'icon':2}); }else{ layer.msg(res.msg,{'icon':1}); setTimeout(function(){parent.window.location.reload();},1000); } },'json'); } </script>