Re: Add Multiple Items, Qty to Cart from html form
| From: | Jochem Maas | Date: | Thu, 18 May 2006 12:08:46 +0000 |
| Subject: | Re: Add Multiple Items, Qty to Cart from html form | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-236388@lists.php.net to get a copy of this message | ||
Wolf wrote:
Andras, <input type=hidden name=item[] value=apples>Apples <input type=text name=qty[] value=0> <input type=text name=price[] value=0>a useful addition can be to use the item id in the 'key' of the input names (and always quoting your element attributes is highly recommended): <input type="hidden" name="item[18]" value="apples">Apples <input type="text" name="qty[18]" value="0"> <input type="text" name="price[18]" value="0"> that way you can easily fish out the quantities when looping the selected items (example mostly ignores input cleaning/validation for brevity etc - but you shouldn't :-) e.g. get anal about make sure things are actually integers when that is what you require, etc): foreach ($_POST['item'] as $itemId => $itemName) { if (isset($_POST['item'][$itemId]) && ($qty = intval($_POST['item'][$itemId])) { addItem($itemId, $qty); } } another thing that popped into my head was the fact that it's probably not intended behaviour to allow the customer to determine the unit price of an item - but if it is then can I have 10 Plasma Screens at 1 dollar a pop? ;-)
Will get you where you need to go on the HTML side of things, then on the back end you need to process each array. By setting a default value of 0 for the qty, you force users to change the values, but you also keep your arrays intact and easier (IMHO) to deal with. Wolf Andras Kende wrote:Hello, I trying to add multiple items to a shopping cart with selectable quantity and price form text field like.. apple : qty: [__] price: [__] orange : qty: [__] price: [__] <Add Items to Cart> I could add multiple items with checkboxes but without selecting quantity and price.. if (isset($_POST['itemschecked'])) { foreach($_POST['itemschecked'] as $itemschecked => $checkeditems ){ AddItem($checkeditems, 1); } Any help is appreciated.. Thanks, Andras