How do I add an unknown selection of inventory items with a user-supplied quantity to the cart?
I am currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Please note that solutions are not limited to this, but preferred if possible.
I have a large MYSQL widget table and a page that allows the user to search for a specific widget.
Let's say, for example, a user enters a model name for some widget, the search can return 20 different widget models. Each widget model is different, they all have their own widget unit price and widget model name, etc.
Now comes the tricky part (for me anyway), the user should be able to enter the quantity next to the desired textbox of the html widget.
The user should then be able to click on the add to cart button, which stores the quantity and the model it is associated with.
(Design note: MYSQL query is used to return a list of search results, and I am using PHP to iterate over the rows of found widgets. At each iteration, I also show a text box to enter the desired amount.).
My main problem is figuring out how to link the MYSQL widget name to the amount entered by the user and store it together in the trash can. Can anyone point me in the right direction, I'm not sure how.
I thought I could replicate the widget model in the Id textbox, but then how would I determine after posting which model had the count ... This is probably not the best way to do this, even if it is possible.
thanks
enter something like [uniq_module_id] in the text box
<input type="text" name="items[10001]">
<input type="text" name="items[10002]">
<input type="text" name="items[10003]">
then in php
$items = $_POST['items'];
print_r($items);
You'll get
[items] => Array
(
[uniq_module_id] => quantity
...
)
and sample
$items = (isset($_POST['items'])) ? $_POST['items'] : array();
if (is_array($items)) {
foreach ($items as $module_id => $quantity)
{
if (intval($quantity) > 0) {
add2cart($module_id, $quantity);
}
}
}
a source to share
Does each widget have an "add to cart" button / link? Create a button / link that will send the required information (number and widget id) to add-to-cart.php file.
foreach ($rows as $row) {
// Print widget info
echo '<input type="text" name="quantity" id="widget_' . $row['widgetID'] . '_qty" value="1">';
echo "<button onclick=\"javascript: location.href='add-to-cart.php?widgetId=" . $row['widgetID'] . "&quantity=' + document.getElementById('widget_" . $row['widgetId'] . "_qty').value;\" value=\"Add to cart\" />";
}
Or something similar. Using Ajax might be a good solution.
a source to share