doulaozhi6835 2017-02-09 11:19
浏览 57

(PHP)包含带复选框的表的发布表单

Let's assume I have a table in a form, containing inputs and checkboxes:

<form method='post' action=''>
    <table>
        <tr>
            <th>Code</th>
            <th>Description</th>
            <th>Select</th>
        </tr>
        <tr>
            <td><input name=table[code][] type='text' value='C001' /></td
            <td><input name=table[description][] type='text' value='Object 1' /></td>
            <td><input name=table[select][] type='checkbox' checked /></td>
        </tr>
        <tr>
            <td><input name=table[code][] type='text' value='C002' /></td
            <td><input name=table[description][] type='text' value='Object 2' /></td>
            <td><input name=table[select][] type='checkbox' /></td>
        </tr>
        <tr>
            <td><input name=table[code][] type='text' value='C003' /></td
            <td><input name=table[description][] type='text' value='Object 3' /></td>
            <td><input name=table[select][] type='checkbox' checked /></td>
        </tr>

    </table>
</form>

The table is populated by PHP and extended by the user (through a jQuery script), so I cannot know how many rows I'll have when the form is posted

in $_POST I find these values (not PHP code, just showing $_POST content):

$_POST['table']['code'][0] = C001
$_POST['table']['code'][1] = C002
$_POST['table']['code'][2] = C003

$_POST['table']['description'][0] = Object 1
$_POST['table']['description'][1] = Object 2
$_POST['table']['description'][2] = Object 3

$_POST['table']['select'][0] = 1
$_POST['table']['select'][1] = 1

As you can see, I have no correlation between 'select' fields and 'code/description'

My solution was to add the index of the array, like this:

<td><input name=table[code][1] type='text' value='C001' /></td>
<td><input name=table[description][1] type='text' value='Object 1' /></td>
<td><input name=table[select][1] type='checkbox' checked /></td>

and checking if(isset(&_POST['table']['select'][1]))

but this doesn't feel an elegant solution to me, as it require to number the fields as the table is populated and update numbers in jQuery as new rows are added.

Is there a better solution for keeping each row's input together?

  • 写回答

2条回答 默认 最新

  • dpvm7231 2017-02-09 11:36
    关注

    I think your solution is the best one, you have to group your correlated fields together, and, since the checkbox element arent sent if not checked, the best way to do that is with a unique id.

    Still, there are other solutions. Off the top of my head, there's this one, which involves a bit of jQuery and is ugly as f**k, but should work :

    <form method='post' action=''>
        <table>
            <tr>
                <th>Code</th>
                <th>Description</th>
                <th>Select</th>
            </tr>
            <tr>
                <td><input name="table[code][]" type='text' value='C001' /></td>
                <td><input name="table[description][]" type='text' value='Object 1' /></td>
                <td>
                    <input type="checkbox" checked />
                    <input name="table[checkbox][]" value="1" />
                </td>
            </tr>
            <tr>
                <td><input name="table[code][]" type='text' value='C002' /></td>
                <td><input name="table[description][]" type='text' value='Object 2' /></td>
                <td>
                    <input type='checkbox' checked />
                    <input name="table[checkbox][]" value="0" />
                </td>
            </tr>
            <tr>
                <td><input name="table[code][]" type='text' value='C003' /></td
                <td><input name="table[description][]" type='text' value='Object 3' /></td>
                <td>
                    <input type='checkbox' checked />
                    <input name="table[checkbox][]" value="1" />
                </td>
            </tr>
        </table>
    </form>
    
    <script type="text/javascript">
        $('input[type="checkbox"]').on('change', function() {
            var target = $(this).parent('td').find('input[type="hidden"]');
            if ($(this).is(':checked')) {
                target.val(1);
            } else {
                target.val(0);
            }
        });
    </script>
    

    I deleted the name attributes from your checkboxes and added hidden fields with the same name.

    When a checkbox's state changes (from checked to unchecked and vice-versa), the jQuery bit will set the corresponding hidden input value to either 1 (if the checkbox is cheked) or 0.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)