php 来自表单的 POST 的 Foreach 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:34:18  来源:igfitidea点击:

Foreach value from POST from form

phppostwhile-loop

提问by Andelas

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.

我将一些数据从表单发布到另一个页面。这是一个购物车,提交的表单在页面上生成之前取决于购物车中有多少项目。例如,如果只有 1 个项目,那么我们只有字段名称“item_name_1”,它应该存储一个值,如“Sticker”和“item_price_1”,它存储该项目的价格。但是如果有人有 5 个项目,我们将需要 'item_name_2'、'item_name_3' 等来获取每个项目的值,直到第 5 个。

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.

我对这种用法还是比较陌生,所以我不完全是如何处理它的最佳方式。

Thanks.

谢谢。

回答by Alp

First, please do not use extract(), it can be a security problem because it is easy to manipulate POST parameters

首先,请不要使用extract(),它可能是一个安全问题,因为它很容易操纵POST参数

In addition, you don't have to use variable variable names (that sounds odd), instead:

此外,您不必使用变量变量名(听起来很奇怪),而是:

foreach($_POST as $key => $value) {
  echo "POST parameter '$key' has '$value'";
}

To ensure that you have only parameters beginning with 'item_name' you can check it like so:

为了确保您只有以“item_name”开头的参数,您可以像这样检查它:

$param_name = 'item_name';
if(substr($key, 0, strlen($param_name)) == $param_name) {
  // do something
}

回答by Dr.Molle

Use array-like fields:

使用类似数组的字段:

<input name="name_for_the_items[]"/>

You can loop through the fields:

您可以遍历字段:

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

回答by Wynn

If your post keys have to be parsed and the keys are sequences with data, you can try this:

如果你的 post 键必须被解析并且键是带有数据的序列,你可以试试这个:

Post data example: Storeitem|14=data14

发布数据示例:Storeitem|14=data14

foreach($_POST as $key => $value){
    $key=Filterdata($key); $value=Filterdata($value);
    echo($key."=".$value."<br>");
}

then you can use strpos to isolate the end of the key separating the number from the key.

那么您可以使用 strpos 来隔离将数字与键分开的键的末尾。

回答by Jase

i wouldn't do it this way

我不会这样做

I'd use name arrays in the form elements

我会在表单元素中使用名称数组

so i'd get the layout

所以我会得到布局

$_POST['field'][0]['name'] = 'value';
$_POST['field'][0]['price'] = 'value';
$_POST['field'][1]['name'] = 'value';
$_POST['field'][1]['price'] = 'value';

then you could do an array slice to get the amount you need

那么你可以做一个数组切片来获得你需要的数量