Marek Buchtela
Marek Buchtela

Reputation: 1003

Processing a form with multiple multiple selects

I have a form for registering people to events, while the user can add or remove registered people - the number of inputs is dynamic. Now, the regular inputs are easy to process, it's just

<input type="text" name="first_name[]">

And I process it as an array in a loop for each participant. However, I need to have a multiple select input for each person, like

<select multiple name="extras[]">
<option value="42">Some extra feature</option>
...</select>

But that of course puts all the selected options into one array and I can't recognize which user picked which options. I also tried name="extras[][]", but that just puts each value into a separate array. How can I make the form/the PHP script a way that I can tell who chose what?

Thanks

Upvotes: 0

Views: 83

Answers (1)

hakki
hakki

Reputation: 6521

send user specific info with option value like

<select multiple name="extras[]">
    <option value="42###USER_ID">Some extra feature</option>
</select>

then explode it on your php script like

foreach($_POST["extras"] as $a->$b){
   $userAndExtras = explode("###",$b);
   $userId = $userAndExtras[1]; // gives USER_ID
   $anotherValue = $userAndExtras[0]; // gives 42 or something like that

}

--

or use hidden fields

Upvotes: 1

Related Questions