前台页面:
模板用的是smatey
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function(){
$("#add").click(function(){
$(this).parent().parent().before('<tr><td><input type="checkbox"/></td><td><input type="text" name="sort[]" size="3" /></td><td><input type="text" name="name[]" size="10"/></td><td><input type="text" name="url[]" size="30" /></td></tr>')
})
$("#del").click(function(){
$("input[name='box[]']").each(function(){
$(this).attr("checked",!$(this).attr("checked"))
})
})
})
</script>
</head>
<body>
<form action="info" method="post">
<table>
<th></th>
<th>显示顺序</th>
<th>站点名称</th>
<th>站点URL</th>
{foreach $res as $val}
<tr>
<td style="display: none"><input type="hidden" name="ids[]" value="{$val.id}"/></td>
<td><input type="checkbox" name="box[]" value="{$val.id}"/></td>
<td><input type="text" value="{$val.sort}" name="sort[]" size="3" /></td>
<td><input type="text" value="{$val.name}" name="name[]" size="10"/></td>
<td><input type="text" value="{$val.url}" name="url[]" size="30" /></td>
</tr>
{/foreach}
<tr>
<td colspan="4"><a href="javascript:void(0)" id="add">添加友情链接</a></td>
</tr>
</table>
<input type="checkbox" id="del"/>删
<input type="submit" value="提交"/>
</form>
</body>
</html>
后台处理用的自己简单封装的一套MVC框架:
<?php
class IndexController extends BaseController{
public function index(){
$link = new LinkModel();
$res = $link->select();
$this->assign('res',$res);
$this->display('index/index.html');
}
public function info(){
$link = new LinkModel();
if(isset($_POST['box'])){
foreach($_POST['box'] as $key => $val){
$link->del(['id' => $val]);
}
}
unset($_POST['box']);
$temp=array();
foreach($_POST as $key=>$val){
foreach ($val as $k=>$v){
$temp[$k][$key] = $v;
}
}
foreach($temp as $key => $val){
if(array_key_exists('ids',$val)){
foreach($val as $k => $v){
$link->save(['id' => $val['ids']],[$k => $v]);
}
}else{
$link->add($val);
}
}
header('location:index');
}
}