从表单中的POST获取Foreach值 [英] Foreach value from POST from form

查看:125
本文介绍了从表单中的POST获取Foreach值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将某些数据从表单发布到另一页.这是一个购物车,并且要根据购物车中有多少商品在页面上生成要提交的表单.例如,如果只有1个商品,那么我们只有字段名称"item_name_1",该字段应存储"Sticker"之类的值,而"item_price_1"则存储该商品的价格.但是,如果某人有5个商品,我们将需要'item_name_2','item_name_3'等,以获取每个商品的价值,直至第五个商品.

I post some data over to another page from a form. It's a shopping cart, and the form that's being submitted is being generated on the page before depending on how many items are in the cart. For example, if there's only 1 items then we only have the field name 'item_name_1' which should store a value like "Sticker" and 'item_price_1' which stores the price of that item. But if someone has 5 items, we would need 'item_name_2', 'item_name_3', etc. to get the values for each item up to the fifth one.

循环浏览这些项目以获得值的最佳方法是什么?

What would be the best way to loop through those items to get the values?

这是我所拥有的,显然不起作用.

Here's what I have, which obviously isn't working.

extract($_POST);

$x = 1; // Assuming there's always one item we're on this page, we set the variable to get into the loop

while(${'item_name_' .$x} != '') {

echo ${'item_name' .$x};

$x++;

}

我对这种用法还比较陌生,所以我并不完全了解如何处理这种用法.

I'm still relatively new to this kind of usage, so I'm not entirely how the best way to deal with it.

谢谢.

推荐答案

使用类似数组的字段:

<input name="name_for_the_items[]"/>

您可以遍历以下字段:

foreach($_POST['name_for_the_items'] as $item)
{
  //do something with $item
}

这篇关于从表单中的POST获取Foreach值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆