How to retrieve multiple text box values from sing

2019-08-26 19:52发布

问题:

I am working on an eCommerce website.I have a product page where i have single form to add multiple product quantities at the same time using the button . On my products page,within my single form i am sending individual product values to next page like these using the loop:

<?
$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?>" />    
<? 
 }
 ?>

Where :

1) the "quantity" field is the quantity of the product a customer will order.

2) the "num_pallets" field is the pallet quantity of the product a product has in the database.

3) the "num_packs" field is the pack quantity of the product a product has in the database.

Now,i want to retrieve the corresponding values on shopping cart page so that i could perform calculations accordingly.

The other problem i have is that when i submit my form,it submits all the values INCLUDING THE products' values of those I dont enter quantity in the text boxes.

MY Question:

How can i loop through the products (I enter the "quantity" in text boxes) on my page so that it could give me the corresponding values of the ordered products ONLY i.e if i fill in the quantity text box of product(11760) then i want to process values of 11760 ,not other products.

How can i retrieve the corresponding values of 1 or more ordered product only ??Thanks

回答1:

If I understand correctly, you want to check whether a quantity was filled in for any number of products, if so, process accordingly, right?

// 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:

Here is the solution.I have worked it out ,tested and found that in order to keep corresponding indexes together with the individual products,I will need to pass the product code in the form of multidimensional array like this:

  <?
   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);
               }
         } 
      }

}
?>