如何获取多值多字段 [英] How to get the Multi Values multi fields

查看:69
本文介绍了如何获取多值多字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我重复了相同的字段,因为如果用户有多个项目要发送第一个项目,而他又一次又一次地重复,则我不是用户,我希望从第一时间开始就添加所有项目他有,所以我不止一次地将相同的必填字段放入.

i have a form and I repeate the same fields because i don't the user if he has more than one Item to send the first Item and he do agin and agin i want it from the first time to add the all items he has, so i put the same Required fields more than once.

<form>
    <select name="items[]">
    <option value="clothes">Clothes</option>
    <option value="shoes">Shoes</option>
    </select>
    <input type="text" name="color[]" />

    <select name="items[]">
    <option value="clothes">Clothes</option>
    <option value="shoes">Shoes</option>
    </select>
    <input type="text" name="color[]" />
</form>

我知道如何只获得一个像这样的字段:name ="color []"我可以通过以下方式获得结果:

and I know how to get just one field like this : name="color[]" I can get the result with:

while(list($key,$value) = each($_POST['color'])){

if(!empty($value)){

echo $_POST['color'][$key];
echo "<br />" ;

}}

但是现在我想获取所有值选择项目和颜色"

but now i want to get all values "select items and color"

我在这里编写了这段代码,我得到了所有结果 但是它没有相互链接!!!

i made this code here and I got all the results But it is not linked to each other!!!

像这样: 衣服 鞋 红色的 蓝色

like this: clothes shoes red blue

我想要这样: 衣服红色 鞋蓝

i want it like this: clothes-red shoes-blue

这是我编写的代码,但我不满意

this is the code which i made But I'm not satisfied

while(list($key,$value) = each($_POST['input'])){

if(!empty($value)){

echo $_POST['input'][$key];
echo "<br />" ;

}}

while(list($key,$value) = each($_POST['items'])){

if(!empty($value)){

echo $_POST['items'][$key];
echo "<br />" ;

}}

有人可以帮我吗:)

谢谢

推荐答案

将您的选择和输入名称更改为这样

Change your select and input names to be like this

<select name="items[0][type]">
<input name="items[0][color]">

<select name="items[1][type]">
<input name="items[1][color]">

然后您可以使用

foreach ($_POST['items'] as $item) {
    $type = $item['type'];
    $color = $item['color'];
}

编辑忘记为每对添加索引以将它们分组在一起

Edit Forgot to add an index to each pair to group them together

这篇关于如何获取多值多字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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