开发者

How to get the Multi Values multi fields

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>

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['i开发者_运维技巧nput'])){

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 />" ;

}}

can anybody help me please :)

thank you,


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]">

Then you can iterate each pair together using

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

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


You have two arrays:

$items = array(0 => 'clothes', 1 => 'shoes')
$color = array(0 => 'blue', 1 => 'red')

Looping through both arrays in succession will of course give you "clothes, shoes, blue, red". Since index 0 of $items corresponds to index 0 of $color, you'll need to access the corresponding index in both arrays at once:

for ($i = 0; $i < count($items); $i++) {
    echo $items[$i] . ' - ' . $color[$i];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜