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

Asked

Viewed 3,036 times

2

I’m creating a form and for some fields I need to submit more than one value and I don’t know if the form I’m doing is the best 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 going through so much ID as to the NOME field and I am separating by two Pipes (||) , and I am doing this because I need to save both the id and the name that was at the time when saving.

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

  • 1

    What do you want to pass the NAME? The ID was not enough?

  • 1

    Have you tried it like this? It might be a good idea to test what you’re trying to do

  • The name I need to know what it was at the time, and that is what matters most, because if it is a value and that value changes in a future, it does not affect the data saved in the past, and guarding the id I still have a comparability of what it was and what it is at the time.

  • @Jorgeb. I’ve tested yes and it works, but I don’t know if there’s any other more correct way to do it!

  • I’d rather spend just the ID, because my NOME are usually in a table in the database. tabela_itens with ID and NOME

  • I also have a habit of doing it that way, and I think most of them are, but this is a special case.

  • @Marcelodiniz with this treatment of the answer would be better to have a table, OR create constants to do this (in case it is PHP create define())

Show 2 more comments

1 answer

3


What you have done is not wrong. It is very common to separate values by , as well. But I believe the most appropriate way is to spend a JSON. 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 such as

<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 the JSON.stringify() to convert its objects into string.

  • Good Bruno, it’s something I thought too, but tell me one thing, anyway it’s a string, and on the server side I have to give a json_decode or something like that?

  • yes it is still necessary this treatment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.