php批量处理POST值的方法

发布时间:2019-11-03编辑:脚本学堂
本文介绍一个php实例,学习下如何批量处理POST数据,有兴趣的朋友一起研究下。

学习php批量处理POST数据的方法,如下图:
x3.jpg

需要同时删除多项,或同时修改多项记录

要点:
1,前端表单中name要加[],如:<input type="hidden" name="id[]" value="{$vo.id}">
2,后端接受如:$id = $_POST [id] [$i];     $i为for中的循环变量,当$i相同时可以使得$_POST [id] [$i]和$_POST [name] [$i]为一一对应;

一,前端表单:
 

复制代码 代码示例:
<form name="form1" method="post" action="__GROUP__/Repair/updateList">
  对所选操作:<input type="submit" value="保 存" class="btn btn-primary"> <br>
            <table id="checkList" class="list table table-hover">
                <tr>
                    <th scope="col"><input id="check" type="checkbox"
                        onclick="checkAll()" /> 全选</th>
                    <th scope="col">主题</th>
                    <th scope="col">校区</th>
                    <th scope="col">房间</th>
                    <th scope="col">报修人</th>
                    <th scope="col">联系电话</th>
                    <th scope="col">报修时间</th>
                    <th scope="col">审核</th>
                    <th scope="col">报修状态</th>
                </tr>
                <volist name="list" id="vo">
                <tr>
                    <td><input type="checkbox" name="key" value="{$vo.id}">{$vo.id}<input type="hidden" name="id[]" value="{$vo.id}"></td>
                    <td>{$vo.topic}</td>
                    <td>{$vo.xiaoqu}</td>
                    <td>{$vo.room}</td>
                    <td>{$vo.pname}</td>
                    <td>{$vo.phone}</td>
                    <td>{$vo.create_time|date='Y-m-d H:i:s',###}</td>
                    <td><select name="check[]"><switch name="vo.check">
                            <case value="0">
                            <option value="0" selected="selected">未审核</option>
                            <option value="1">审核</option>
                            </case> <default />
                            <option value="0">未审核</option>
                            <option value="1" selected="selected">审核</option>
                            </switch></select></td>
                    <td><select name="status[]"><switch name="vo.status">
                            <case value="已修">
                            <option value="未修">未修</option>
                            <option value="任务已下">任务已下</option>
                            <option value="已修" selected="selected">已修</option>
                            </case> <case value="任务已下">
                            <option value="未修">未修</option>
                            <option value="任务已下" selected="selected">任务已下</option>
                            <option value="已修">已修</option>
                            </case> <default />
                            <option value="未修" selected="selected">未修</option>
                            <option value="任务已下">任务已下</option>
                            <option value="已修">已修</option>
                            </switch></select></td>
                </tr>
                </volist>
            </table>
        </form>

二,服务端处理代码
 

复制代码 代码示例:
<?php
/**
     * 批量修改状态
     */
    public function updateList() {
        $result = false;
        for($i = 0; $i < count ( $_POST [id] ); $i ++) {
           
            $id = $_POST [id] [$i];
            $check = $_POST [check] [$i];
            $status = $_POST [status] [$i];
           
            // 保存登录信息
            $Repair = M ( 'Repair' );
            $data ['id'] = $id;
            $data ['check'] = $check;
            $data ['status'] = $status;
            $result = $Repair->save ( $data );
        }
       
        if (false !== $result) {
            $this->success ( '修改成功!' );
        } else {
            $this->error ( '修改失败!' );
        }
    }