如何使用用户提供的数量将未知选择的库存项目添加到购物车?

时间:2021-12-01 20:41:43

I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that solutions are not limited to this but preferred if possible.

我目前正在使用PHP,JAVASCRIPT,MYSQL,XHTML,CSS来开发我的网站。注意,解决方案不限于此,但如果可能则优选。

I have a large MYSQL table of widgets and a page that allows the user to search for a specific widget.

我有一个大的MYSQL小部件表和一个允许用户搜索特定小部件的页面。

Let's say for example the user types the model name for some widget, the search could return 20 different widget models. Each widget model is different, they all have their own widget unit price and widget model name, etc.

例如,假设用户键入某个小部件的型号名称,搜索可以返回20个不同的小部件模型。每个小部件模型都不同,它们都有自己的小部件单价和小部件模型名称等。

Now comes the tricky part (for me anyways), the user should be able to enter a quantity next to a desired widget html text field.

现在是棘手的部分(对我来说无论如何),用户应该能够在所需的小部件html文本字段旁边输入数量。

The user then should be able to push an add to cart button that stores the quantity and the model the quantity it's associated with.

然后,用户应该能够按下添加到购物车按钮,该按钮存储数量,模型与其关联的数量。

(Design note: A MYSQL query is used to return a list of the search results and I use PHP to echo back the rows of widgets found. Upon each iteration I also display the text field where the desired quantity should be entered. ).

(设计说明:MYSQL查询用于返回搜索结果列表,我使用PHP回显找到的小部件行。每次迭代时,我还会显示应输入所需数量的文本字段。)。

My main problem is figuring out how to associate the MYSQL drawn widget name with the quantity entered by the user and store it together in the cart. Could someone point me in the right direction, I'm not sure how to go about tackling this.

我的主要问题是弄清楚如何将MYSQL绘制的小部件名称与用户输入的数量相关联,并将它们一起存储在购物车中。有人能指出我正确的方向,我不知道如何解决这个问题。

I was thinking I might be able to echo the widget model into text field Id , but then how would I determine after post which model had a quantity... This is probably not the best means to do it, even if it was possible.

我以为我可能能够将小部件模型回显到文本字段Id中,但之后我如何确定哪个模型具有数量...这可能不是最好的方法,即使它是可能的。

Thanks

2 个解决方案

#1


make the textfield names something like items[uniq_module_id]

使文本域名称类似于项目[uniq_module_id]

<input type="text" name="items[10001]">
<input type="text" name="items[10002]">
<input type="text" name="items[10003]">

then in php

然后在PHP中

$items = $_POST['items'];
print_r($items);

you will get

你会得到

[items] => Array
    (
        [uniq_module_id] => quantity
            ...
    )

and a 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);
        }
    }
}

#2


Is there a "add to cart"-button/link at every widget? Generate a button/link that send the needed info (quantity and widget ID) to an add-to-cart.php.

每个小部件都有“添加到购物车” - 按钮/链接吗?生成一个按钮/链接,将所需信息(数量和小部件ID)发送到add-to-cart.php。

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 alike. Using Ajax would maybe be a good solution as well.

或类似的东西。使用Ajax也许是一个很好的解决方案。

#1


make the textfield names something like items[uniq_module_id]

使文本域名称类似于项目[uniq_module_id]

<input type="text" name="items[10001]">
<input type="text" name="items[10002]">
<input type="text" name="items[10003]">

then in php

然后在PHP中

$items = $_POST['items'];
print_r($items);

you will get

你会得到

[items] => Array
    (
        [uniq_module_id] => quantity
            ...
    )

and a 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);
        }
    }
}

#2


Is there a "add to cart"-button/link at every widget? Generate a button/link that send the needed info (quantity and widget ID) to an add-to-cart.php.

每个小部件都有“添加到购物车” - 按钮/链接吗?生成一个按钮/链接,将所需信息(数量和小部件ID)发送到add-to-cart.php。

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 alike. Using Ajax would maybe be a good solution as well.

或类似的东西。使用Ajax也许是一个很好的解决方案。