用form表单实现一键批量增、删、改功能

前台页面:

模板用的是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');

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值