IndexController.class.php代码
public function index(){
$this->user=M('User')->select();
$this->display();
}
//添加栏目
public function add(){
$this->display();
}
//保存栏目数据
public function areate(){
if(M('User')->add($_POST)){
$this -> success('添加成功!','index');
} else {
$this -> error('添加失败!');
}
}
public function edit(){
$id = $_GET['id'];
$this->user =M('User')->find($id);
$this->display();
}
public function updates(){
$data['id']=$_POST['id'];
$data['username']=$_POST['username'];
$arr->data=M('User')->save($data);
if($arr>0){
$this -> success('修改成功!','index');
}else{
$this -> error('修改失败!');
}
}
index.html代码<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 引入 jquery 和 layer 插件 -->
<script src="http://res.xiami.net/pc/lay/lib.js"></script>
<script src="__Public__/layer/layer.min.js"></script>
</head>
<body>
<a href="__URL__/add">添加</a>
<table>
<volist id="vo" name="user">
<tr>
<td>{$vo.username}</td>
<td><a href="{:U('Index/edit',array('id'=>$vo['id']))}">修 改</a></td>
</tr>
</volist>
</table>
</body>
</html>
add.html代码<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 引入 jquery 和 layer 插件 -->
<script src="http://res.xiami.net/pc/lay/lib.js"></script>
<script src="__Public__/layer/layer.min.js"></script>
<style type="text/css">
.btn{
width: 120px;
height: 40px;
background: #000;
color: #fff;
display: block;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<form class="form-horizontal" id="form" action="{:U('Index/areate')}" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">用户名</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="username" placeholder="请输入用户名" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-primary" id="submit">提 交</a>
</div>
</div>
</form>
<script>
// ajax 提交表单
$(document).ready(function(){
$("#submit").click(function(){ // 提交按钮触发事件
var tourl = $("#form").attr("action"); // 获取 表单的 提交地址
// 序列化 表单数据 后提交 ,太简洁了
$.post(tourl,$("form").serialize(),function(data){
//直接返回首页
/*layer.msg(data['info'],5,data['status'],function(){
location.href = "{:U('Index/index')}"; //自动关闭后返回首页
});*/
//确定返回首页还是再次添加
$.layer({
shade: [0.5, '#000'], //弹出层背景颜色
dialog: {
msg: data['info'], //读取后台返回信息
btns: 2, //设置两个按钮
type: data['status'], //通过返回的代码设置图标 1或0
btn: ['返回首页','再次添加'],
yes: function(){
location.href = "{:U('Index/index')}"; //关闭后返回首页
}, no: function(){
location.reload(); //关闭后刷新页面
}
}
});
});
});
});
</script>
</body>
</html>

layer-v1.8.5.zip
( 67.01 KB 下载:361 次 )