dongsiju1941 2018-06-29 06:45
浏览 103
已采纳

获取具有相同名称的所有复选框(已选中或未选中)的值

I am trying to build a form that lists all available items and users can select some items and enter a value to input element aside the checkbox.

Each checkbox has a input text box aside it.

<?php
    foreach ($items as $item):
        $itemID = $item['item_id'];
        $itemTitle = $item['item'];
?>
    <tr>
        <td>
            <li> 
                <div class="checkbox">
                    <input type="hidden" name="selected_items[]" value="0">
                    <input type='checkbox' name='selected_items[]' value='<?php echo $itemID; ?>'/> 
                <?php echo ucfirst($itemTitle); ?>

                </div>
            </li>
        </td>
        <td><input type='number' name='quantities[]' value="0.00" step="0.01" /> </td>
    </tr>
<?php endforeach; ?>

Here's how the form looks:

Available Items    |  Qty
--------------------------
[] Bread           |  [input]
[] Coffee          |  [input]
[] Egg             |  [input]
[] Cake            |  [input]

I want the two arrays : selected_items[] and quantities to be of the same length as they are so I can combine them.

If unchecked, value should be left to 0.

So, combined_array should look like:

0=>0
Coffee=>44
Egg=>56
0=>0

Can anyone help me with this?

  • 写回答

1条回答 默认 最新

  • douzhuan0309 2018-06-29 07:10
    关注

    Without more detail, this example assumes you want the "combined_array" to set in your $_POST data...

    Firstly I would remove the "hidden" selected_items input. As these are an array of inputs, the index keys would be out of alignment.

    Setting the index key to a specific value for selected_items, quantities and adding a new input item, should help correlate your $_POST data like so:

    <?php foreach ($items as $item):
        $itemID = $item['item_id'];
        $itemTitle = $item['item'];
    ?>
        <tr>
            <td>
                <li> 
                    <div class="checkbox">
                        <input type='checkbox' name='selected_items[<?php echo $itemID; ?>]' value='<?php echo $itemTitle; ?>'/>  <!-- Give your input $itemID key -->
                        <?php echo ucfirst($itemTitle); ?>
                    </div>
                </li>
            </td>
            <td><input type='number' name='quantities[<?php echo $itemID; ?>]' value="0.00" step="0.01" /> </td>  <!-- Give your input $itemID key -->
        </tr>
        <input type='hidden' name='available_items[<?php echo $itemID; ?>]' value="<?php echo $itemTitle; ?>" />   <!-- Give your input $itemID value -->
    <?php endforeach; ?>
    
    
    <?php
    if(!empty($_POST)){
        foreach($_POST['available_items'] as $itemID=>$itemTitle){ // <-- Loop through your "available_items"
            $itemTitle = (!empty($_POST['selected_items'][$itemID])?$itemTitle:0); // <-- this changes the itemTitle based on whether !empty($_POST['selected_items'][$itemID]) 
            $Qty = (!empty($_POST['selected_items'][$itemID])&&!empty($_POST['quantities'][$itemID])?$_POST['quantities'][$itemID]:0); // <-- Checks for quantities 
            $CombinedArray[$itemTitle] = $Qty;
        }
        print_r($CombinedArray);
    }
    ?>
    

    Your result would be something like this:

    "0"=>0
    "Coffee"=>44
    "Egg"=>56
    

    This is because you have the potential for creating duplicate keys in your $CombinedArray.

    Removing this line $itemTitle = (!empty($_POST['selected_items'][$itemID])?$itemTitle:0);:

    <?php
    if(!empty($_POST)){
        foreach($_POST['available_items'] as $itemID=>$itemTitle){ // <-- Loop through your "available_items"
            $Qty = (!empty($_POST['selected_items'][$itemID])&&!empty($_POST['quantities'][$itemID])?$_POST['quantities'][$itemID]:0); // <-- Checks for quantities 
            $CombinedArray[$itemTitle] = $Qty;
        }
        print_r($CombinedArray);
    }
    ?>
    

    Would result in something like:

    "Bread"=>0
    "Coffee"=>44
    "Egg"=>56
    "Cake"=>0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效