使用php从HTML表单中分组关联数组

时间:2014-01-03 06:43:21

标签: php html html-form

我正在研究基于表格的网络表单。每行都有单独的数据。在填写我的表单后,所有字段都应该在php中分组为关联数组。但经过多次试验,我没有得到确切的结果。 enter image description here

生成的表单

            <table width="60%" class="table table-striped table-bordered table-hover att  doodleTable">
                <thead>
                    <tr>
                        <th width="2%">ID</th>
                        <th width="10%"> Emp ID / Name </th>
            <!--                                        <th width="10%"></th>-->
                        <th width="3%">Site</th>
                        <th width="5%"> Attendance </th>
                        <th width="3%">Salary/hr</th>
                        <th width="5%">In time</th>
                        <th width="5%">Out time</th>
                        <th width="3%">Lunch Time</th>
                        <th width="3%">Over Time</th>
                        <th width="3%">Advance Amt</th>
                        <th width="3%">Total time</th>
                    </tr>
                </thead>
                <form method="POST" action="http://example.com/attendance/update" accept-charset="UTF-8"><input name="_token" type="hidden" value="ZZVYZejVpAkVfjeTcs58q8JXwrcptOsinTNr6Xha">                                    <tbody>
                        <tr>
                            <th width="2%">2</th>
                            <th width="10%"> SEO067 / Nagaraj </th>
                    <input  style="display:none" type="text" name="att[emp_id]" value='SEO067'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[SEO067][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[SEO067][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[SEO067][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[SEO067][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[SEO067][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[SEO067][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067][twt]" class="form-control twt">  </th>
                    </tr>
                    <tr>
                        <th width="2%">3</th>
                        <th width="10%"> SEO067rrr / Nagaraj </th>
                    <input  style="display:none" type="text" name="att[emp_id]" value='SEO067rrr'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[SEO067rrr][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[SEO067rrr][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[SEO067rrr][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[SEO067rrr][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[SEO067rrr][twt]" class="form-control twt">  </th>
                    </tr>
                    <tr>
                        <th width="2%">4</th>
                        <th width="10%"> WEB001 / suresh </th>
                    <input  style="display:none" type="text" name="att[emp_id]" value='WEB001'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[WEB001][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[WEB001][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[WEB001][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[WEB001][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[WEB001][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[WEB001][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[WEB001][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[WEB001][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[WEB001][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[WEB001][twt]" class="form-control twt">  </th>
                    </tr>
                    </tbody>
            </table>
            <input class="btn btn-primary pull-right" type="submit" value="Submit">                                </form>                            </div>

上面的表格没有给出我期望的多维数组。 我需要的多维数组如下

            Array
            (
                [0] => Array
                    (
                        [emp_id] => WEB001
                        [dt] => 03/01/2014
                        [site] => 03/01/2014
                         ...
                    )
                 [1] => Array
                    (
                        [emp_id] => WEB002
                        [dt] => 03/01/2014
                        [site] => 03/01/2014
                      ...
                    )
                 [2] => Array
                    (
                        [emp_id] => WEB002
                        [dt] => 03/01/2014
                        [site] => 03/01/2014
                    )

                .....
            )

注意:动态生成表单

1 个答案:

答案 0 :(得分:2)

我发布了您希望修改的表单(添加了每个输入标记元素的name属性的索引),希望这个帮助:

        <table width="60%" class="table table-striped table-bordered table-hover att  doodleTable">
                <thead>
                    <tr>
                        <th width="2%">ID</th>
                        <th width="10%"> Emp ID / Name </th>
            <!--                                        <th width="10%"></th>-->
                        <th width="3%">Site</th>
                        <th width="5%"> Attendance </th>
                        <th width="3%">Salary/hr</th>
                        <th width="5%">In time</th>
                        <th width="5%">Out time</th>
                        <th width="3%">Lunch Time</th>
                        <th width="3%">Over Time</th>
                        <th width="3%">Advance Amt</th>
                        <th width="3%">Total time</th>
                    </tr>
                </thead>
                <form method="POST" action="" accept-charset="UTF-8"><input name="_token" type="hidden" value="ZZVYZejVpAkVfjeTcs58q8JXwrcptOsinTNr6Xha">                                    <tbody>
                        <tr>
                            <th width="2%">2</th>
                            <th width="10%"> SEO067 / Nagaraj </th>
                    <input  style="display:none" type="text" name="att[0][emp_id]" value='SEO067'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[0][dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[0][site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[0][SEO067][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[0][SEO067][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[0][SEO067][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[0][SEO067][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[0][SEO067][twt]" class="form-control twt">  </th>
                    </tr>
                    <tr>
                        <th width="2%">3</th>
                        <th width="10%"> SEO067rrr / Nagaraj </th>
                    <input  style="display:none" type="text" name="att[1][emp_id]" value='SEO067rrr'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[1][dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[1][site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[1][SEO067rrr][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[1][SEO067rrr][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[1][SEO067rrr][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[1][SEO067rrr][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[1][SEO067rrr][twt]" class="form-control twt">  </th>
                    </tr>
                    <tr>
                        <th width="2%">4</th>
                        <th width="10%"> WEB001 / suresh </th>
                    <input  style="display:none" type="text" name="att[2][emp_id]" value='WEB001'  hidden="">
                    <th  width="5%" style="display:none"><input type="text" name="att[2][dt]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="5%" style="display:none"><input type="text" name="att[2][site]" class="form-control date-pic-choose" readonly="" hidden=""> </th>
                    <th width="7%"> <input type="text" name="att[2][WEB001][][site]" class="form-control site"> </th>
                    <th width="3%"> 
                        <input type="radio" name="att[2][WEB001][st]" value="p" id="attendance_0">P   
                        <input type="radio" name="att[2][WEB001][st]" value="a" id="attendance_0">A
                    </th>
                    <th width="7%"> <input type="text" name="att[2][WEB001][sh]" class="form-control sal"> </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][wt]" class="form-control intime">  </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][ot]" class="form-control outtime">  </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][lt]" class="form-control lt"> </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][ovrtime]" class="form-control ovrtime">  </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][aa]" class="form-control">  </th>
                    <th width="3%"> <input type="text" name="att[2][WEB001][twt]" class="form-control twt">  </th>
                    </tr>
                    </tbody>
            </table>
            <input class="btn btn-primary pull-right" type="submit" value="Submit">                                </form>                            </div>