//set.php
<?php namespace app\admin\controller;
use app\admin\model\SetModel;
use think\facade\Request;
class Set extends Base{
public function index(){
$res = SetModel::select();
if($res){
$set = [];
foreach ($res as $val) {
$set = json_decode($val,true);
}
//dump(json_decode($set['values'],true));
$this->view->set = json_decode($set['values'],true);
}
return $this->view->fetch();
}
public function save(){
$info['title'] = trim(input('post.title'));
$info['key'] = trim(input('post.key'));
$info['desc'] = trim(input('post.desc'));
$data['names'] = 'setting';
$data['values'] = json_encode($info);
$res = SetModel::where('names',$data['names'])->find();
if($res){
SetModel::where('names',$data['names'])->update($data); }else{ SetModel::insert($data); }
exit(json_encode(['code'=>0,'msg'=>'保存成功!'])); }
}
//index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding:10px;">
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">网站名称</label>
<div class="layui-input-inline">
<input type="text" name="title" class="layui-input" value="{$set.title}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">关键词</label>
<div class="layui-input-inline">
<input type="text" name="key" class="layui-input" value="{$set.key}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">网站描述</label>
<div class="layui-input-block">
<input type="text" name="desc" class="layui-input" value="{$set.desc}">
</div>
</div>
</form>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">提交</button>
</div>
</div>
</body>
</html>
<script type="text/javascript">
layui.use(['form','layer'],function(){
var form = layui.form;
var layer = layui.layer;
$ = layui.jquery; })
function save(){
var title = $('input[name=title]').val();
if(title==''){ layer.msg('网站名称不能为空',{'icon':2}); return; }
$.post('{:url(\'save\')}',$('form').serialize(),function(res){
if(res.code==0){
layer.msg(res.msg,{'icon':1});
setTimeout(function(){window.location.reload()},1000); }
if(res.code>0){ layer.msg(res.msg,{'icon':2}); } }
,'json')
}
</script>