php 尝试接收未知数量的输入

发布于 2024-12-25 12:35:02 字数 722 浏览 1 评论 0原文

购物车的应用程序。 我从 ajax 源加载(任何源都是相同的 xD)和未知数量的服务(未知,因为它们在数据库中的数量不断增长)。 它们由一个复选框表示,指示您是否需要该服务,然后显示 2 个输入,一个输入禁用价格,另一个输入设置数量。

问题是我如何将它们全部发送到 php 进行处理(总价计算和数据库插入)。我已使用带括号的输入属性名称进行多个选择,

<select name="something[]">...  ...</select>

我想知道是否有办法实现类似的功能,但需要输入。 或者可能是 mooitems 中的一些奇特的 jquery 插件。- 这是查看我完全失败尝试的页面:(

http://www.micontrol.cl/~mvargas/wordpress/wp-transatacama/reservas-rapidas/form-reservas-pal.php

(您首先有选择一个viaje,然后选择一个类别,现在将显示关联的服务,最后调整adultos的数量以便能够提交表格...抱歉,流程是一个要求:/)

请帮助我提供一些指导,有什么可读的,什么都可以:D 提前谢谢。

the application sort of a shopping cart.
I load from an ajax source (any source would be the same xD) and unknown number of services (unknown cuz' they grow in number in the database).
they are represented by a check-box which indicate if you want the service, then it displays 2 inputs one with the price disabled and the other to set quantity.

the problem is how do I send them all to php for process (total price calculation and database inserting). I have used the input attribute name with brackets for multiple selects

<select name="something[]">...  ...</select>

I would like to know if there's a way to achieve something like that, but with inputs.
or maybe some fancy jquery plug-in in the kind of mooitems.-
here is the page to see my total fail attempt :(

http://www.micontrol.cl/~mvargas/wordpress/wp-transatacama/reservas-rapidas/form-reservas-pal.php

(you first have to select a viaje then a categoria, the services associated will display now, and finally adjust the qtty of adultos to be able to submit the form... sorry that flow was a requiriment : /).

please help me out with some guidance, something to read, anything :D
thx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2025-01-01 12:35:02

好的,所以如果您有多个要发送到服务器的项目,您必须构建 HTML,使其看起来如下所示(动态内容显示在方括号 [] 中):

<input type="checkbox" name="service_[service_id]" value="[service_id]"/> [service_name]<br/>
<input type="text" value="[price]" disabled/>
<input type="text" name="quantity_[service_id]"/>

您必须构建输入的名称由前缀(如服务或数量)和后缀(即该服务的 ID)组成,这样您就可以在 PHP 中检查它们。

现在在 PHP 中你可以做这样的事情:

<?
    $prefix = "service_";
    foreach ( $_POST as $name => $value )
    {
        if ( substr( $name, 0, strlen( prefix ) ) == $prefix )
        {
            $serviceId = $value;
            $quantity  = $_POST[ "quantity_$serviceId" ];
        }
    }
?>

在这里你可以看到我已经在所有 POST 数据上创建了一个循环,试图查找以前缀 service_ 开头的字段名称,一旦找到一个,我就知道它的值是服务 ID。然后我可以使用服务 ID 来查找与所选服务 ID 一起发送的数量_[service_id] 字段。

我希望这有帮助...

Ok, so if you have multiple items which you want to send to the server, you have to build your HTML to look something like the following (dynamic stuff are shown in square brackets []):

<input type="checkbox" name="service_[service_id]" value="[service_id]"/> [service_name]<br/>
<input type="text" value="[price]" disabled/>
<input type="text" name="quantity_[service_id]"/>

You have to build the names for the inputs composed of a prefix (like service or quantity) and a suffix which is the ID for that service, this way you will be able to check for them in the PHP.

Now in the PHP you could do something like this:

<?
    $prefix = "service_";
    foreach ( $_POST as $name => $value )
    {
        if ( substr( $name, 0, strlen( prefix ) ) == $prefix )
        {
            $serviceId = $value;
            $quantity  = $_POST[ "quantity_$serviceId" ];
        }
    }
?>

Here you can see i've created a loop over all of the POST data trying to find field names which start with the prefix service_ and once i find one, i know that its value is the service ID. Then i can use the service ID in order to find the quantity_[service_id] field which was sent along with the selected service ID.

I hope this helps...

来世叙缘 2025-01-01 12:35:02

为什么不看看jquery的serialize()呢?

http://api.jquery.com/serialize/

然后迭代 $_POST[] 或PHP 中的 $_GET[] 取决于 ajax 调用的方法。

这也可能有用:

http://www.php.net /manual/en/reserved.variables.post.php#84813

Why not look at jquery serialize()?

http://api.jquery.com/serialize/

and then just iterate over $_POST[] or $_GET[] in your PHP depending on the method of your ajax call.

This may also be useful:

http://www.php.net/manual/en/reserved.variables.post.php#84813

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文