【问题标题】:How to retrieve multiple text box values from single form using PHP/Codeigniter?如何使用 PHP/Codeigniter 从单个表单中检索多个文本框值?
【发布时间】:2013-01-17 15:12:50
【问题描述】:

我在一个电子商务网站上工作。我有一个产品页面,我有一个表单可以使用按钮同时添加多个产品数量。 在我的产品页面上,在我的单一表单中,我使用循环将单个产品值发送到下一页:

<?
$query_data=$this->db->get('products');
foreach($query_data->result() as $product)
 {
?>
  <input name="productname[]" value="<?=$product->Name?>" type="hidden" />
 <input type = "hidden" id = "product_id" name = "product_id[]" value = "<?=$product->Code?>" />
  <input name="num_pallets[]" id="num_pallets" value="<?=$pallet?>" type="hidden" />
 <input type = "hidden" id = "num_packs" name = "num_packs[]" value = "<?=$packet?>" />
 <input type="text" name="quantity[]" id="quantity" size="2" />
<input type = "hidden" id = "product_price" name = "product_price" value = "<?=$number?>" />    
<? 
 }
 ?>

地点:

1) “数量”字段是客户将订购的产品数量。

2) “num_pallets”字段是产品在数据库中的产品托盘数量。

3) “num_packs”字段是产品在数据库中的包装数量。

现在,我想检索购物车页面上的相应值,以便进行相应的计算。

我遇到的另一个问题是,当我提交表单时,它会提交所有值,包括我没有在文本框中输入数量的产品值。

我的问题:

如何在我的页面上循环浏览产品(我在文本框中输入“数量”),以便它可以只给我订购产品的相应值,即如果我填写产品的数量文本框( 11760)然后我想处理 11760 的值,而不是其他产品。

如何仅检索 1 个或多个已订购产品的对应值??谢谢

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    如果我理解正确,您想检查是否为任意数量的产品填写了数量,如果是,请进行相应处理,对吗?

    // controller method that receives posted form data
    $quantity = $this->input->post('quantity');
    foreach($this->input->post('product_id') as $key => $id){
        if($quantity[$key]){
            //there's a quantity for this product
        }
    }
    

    【讨论】:

      【解决方案2】:

      解决办法是这样的。我已经解决了,测试发现为了将相应的索引与单个产品保持在一起,我需要像这样以多维数组的形式传递产品代码:

        <?
         if (isset($_POST['quantity']) && is_array($_POST['quantity']))  
        {
      
          $field_array = array();
           foreach($_POST['quantity'] AS $itemID => $value) 
           {  
                  if ($_POST['quantity'][$itemID]>0)
                { 
      
               if (isset($_POST['product_id'][$itemID])&& (isset($_POST['product_price'][$itemID])) && (isset($_POST['counter'][$itemID]))&& (isset( $_POST['quantity'][$itemID])))
      
                    {
      
                      $productid=$_POST['product_id'][$itemID];
                      $this->load->model('m_shopping_cart');
                 $pricehead=($_POST['quantity'][$itemID])*($_POST['product_price'][$itemID]);
                 $this->m_shopping_cart->add_to_cart($_POST['product_id'][$itemID],$_POST['product_price'][$itemID],$_POST['quantity'][$itemID],$_SESSION['cart_id'],$pricehead,false,false,false);
                     }
               } 
            }
      
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多