ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用PHP循环处理多个帖子表单值

2019-10-31 14:33:10  阅读:113  来源: 互联网

标签:codeigniter php


我有一个用于编辑订单的表单.在我的编辑订单页面上,我正在分解订单.例如,订单详细信息(客户名称,地址等)然后订购这样的商品:

订单详细信息:

     <form name="edit_order" id="edit_order" method="post">
      <? 
     if ($order_details) {
                //    print_array($order_details);
                    foreach ($order_details as $key => $link) {
                    ?>
                    <div width="200">
                        <div><b>Product: </b> <br><?=$link->product_name?> - <?=$link->product_id?></div>  
                        <div><strong>Cost:</strong><br>&pound;<?=$link->unit_cost?></div>
                        <div><strong>Pack Qty:</strong><br><input name="quantity" id="quantity" value="<?=$link->quantity?>" style="width:50px;"   /> </div> 
                        <div><strong>Pack Cost:</strong> <br><input name="pack_cost" id="pack_cost" value="<?=$link->pack_cost?>" style="width:50px;" />   </div>
                           <div><strong>Pallet Qty:</strong><br><input name="pallet_quantity" id="pallet_quantity" value="<?=$link->pallet_quantity?>" style="width:50px;" />  </div>
                           <div><strong>Pallet Cost:</strong><br><input name="pallet_quantity" id="pallet_quantity" value="<?=$link->pallet_unit?>" style="width:50px;" />  </div>

                    </div>
                    <?
                    }
                }
           ?>

我的问题是:

如何在下一页的多个字段(输入框)中检索这些值?

例如,如果我在编辑订单页面上编辑了10种产品的“ pallet_quantity”和“ pack_cost”,那么我如何使用循环将它们保存到数据库中呢?

当我尝试在网页上显示诸如print($_ POST [‘pack_cost’])的值时,它给了我一个值.如何循环遍历POST数组以显示并保存编辑后的值到数据库中?
请帮我.

解决方法:

您可以为输入值创建数组.看下面的代码…

<input name="pack_cost[]" id="pack_cost1" value="<?=$link->pack_cost?>" style="width:50px;" />
<input name="pack_cost[]" id="pack_cost2" value="<?=$link->pack_cost?>" style="width:50px;" />
<input name="pack_cost[]" id="pack_cost3" value="<?=$link->pack_cost?>" style="width:50px;" />

只要记住这些输入框的ID应该不同即可.

因此,当您在下一页上执行echo $_POST [‘pack_cost’]时,将得到一个名为pack_cost的数组,其中包含键和值.

已编辑

整个代码:-

<form name="edit_order" id="edit_order" method="post">
<? 
if ($order_details) {
     //    print_array($order_details);
     foreach ($order_details as $key => $link) {
?>
    <div width="200">
        <div><b>Product: </b> <br><?=$link->product_name?> - <?=$link->product_id?></div>  
        <div><strong>Cost:</strong><br>&pound;<?=$link->unit_cost?></div>
        <div><strong>Pack Qty:</strong><br><input name="quantity[]" id="quantity<?php echo $key; ?>" value="<?=$link->quantity?>" style="width:50px;"   /> </div> 
        <div><strong>Pack Cost:</strong> <br><input name="pack_cost[]" id="pack_cost<?php echo $key; ?>" value="<?=$link->pack_cost?>" style="width:50px;" />   </div>
        <div><strong>Pallet Qty:</strong><br><input name="pallet_quantity[]" id="pallet_quantity<?php echo $key; ?>" value="<?=$link->pallet_quantity?>" style="width:50px;" />  </div>
        <div><strong>Pallet Cost:</strong><br><input name="pallet_quantity[]" id="pallet_quantity<?php echo $key; ?>" value="<?=$link->pallet_unit?>" style="width:50px;" />  </div>
    </div>
 <?
    }
 }
 ?>
</form>

这就是将输出您的需求的东西.

下面是获取返回值的代码

if($_POST)
{
    $field_array = array();
    for( $i=0 ; $i < count($_POST['pack_cost']) ; $i++ )
    {
        $field_array[$i]['quantity']        = $_POST['quantity'][$i];
        $field_array[$i]['pack_cost']       = $_POST['pack_cost'][$i];
        $field_array[$i]['pallet_quantity'] = $_POST['pallet_quantity'][$i];
        $field_array[$i]['pallet_cost']     = $_POST['pallet_cost'][$i];

        // if you require then the query for your database
    }
}

标签:codeigniter,php
来源: https://codeday.me/bug/20191031/1976247.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有