Skip to content Skip to sidebar Skip to footer

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 ad

Solution 1:

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


Solution 2:

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];
}

Post a Comment for "How To Get The Multi Values Multi Fields"