How to send more than one value in a select element of form html?

2

I'm creating a form and for some fields I need to send more than one value and I do not know if the form I'm doing is the best one or not.

<select name="itens[]">
   <option value=""></option>
   <option value="1||Primeiro Item">Primeiro Item</option>
   <option value="2||Segundo Item">Segundo Item</option>
   <option value="3||Terceiro Item">Terceiro Item</option>
</select>
So I have this element that in value I'm passing both the ID and the NOME of the field and I'm separating it by two pipes (||), and I'm doing this because I need to store both the id and the name that was in the moment when to save.

My question is the following, does this form work? Yes, because on the other side, I can give an explode, for example and with that separate the values, but it is the correct form or it does not have a correct form, but a better one?!

    
asked by anonymous 18.11.2014 / 12:43

1 answer

2

What you did is not wrong. It is very common to separate values by% with% as well. But I think the most appropriate way is to pass , . You can pass using vector notation as

<select name="itens[]">
   <option value=""></option>
   <option value="[1,Primeiro Item]">Primeiro Item</option>
   <option value="[2,Segundo Item]">Segundo Item</option>
   <option value="[3,Terceiro Item]">Terceiro Item</option>
</select>

or that of an object like

<select name="itens[]">
   <option value=""></option>
   <option value="{'id' : 1, 'desc': 'Primeiro Item'}">Primeiro Item</option>
   <option value="{'id' : 2, 'desc': 'Segundo Item'}">Segundo Item</option>
   <option value="{'id' : 3, 'desc': 'Terceito Item'}">Tericeiro Item</option>
</select>

The second option better describes your data, and you can use JSON to convert your objects to string.

    
18.11.2014 / 12:48