Gin: How to get multiple values from select

Created on 29 Oct 2016  Â·  3Comments  Â·  Source: gin-gonic/gin

Hi everyone!

I am pretty new to Golang, so maybe this is a newbie question.
What I am trying to do – to add tags field, and it is a select2 select box with multiple choices. So, basically, it is the following HTML:

<select name="tags" multiple="multiple">
   <option value="id_1">Prague</option>
   <option value="id_2">Novi Sad</option>
   <option value="id_3">Vienna</option>
</select>

But if I read it through c.PostForm("tags"), it shows me only the first value.

I haven't found the answer, but I've found that I can do the next:

c.Request.ParseMultipartForm(5 * 1024 * 1024)
tags := c.Request.Form["tags"]
fmt.Println(tags)

It works, but I wonder whether it is correct approach or not.

Thank you!

question

Most helpful comment

Hi Bloomca,

At first, you have to modified your select <select name="tags" multiple="multiple"> to <select name="tags[]" multiple="multiple"> because we want to get values selected in an array.

Then, declare a struct for you form.

type Myform struct {
    Tags []string `form:"tags[]"`
}

Witch is more simple for get selected inputs.

var myform Myform
c.Bind(&myform)

fmt.Println(myform.Tags)

All 3 comments

Hi Bloomca,

At first, you have to modified your select <select name="tags" multiple="multiple"> to <select name="tags[]" multiple="multiple"> because we want to get values selected in an array.

Then, declare a struct for you form.

type Myform struct {
    Tags []string `form:"tags[]"`
}

Witch is more simple for get selected inputs.

var myform Myform
c.Bind(&myform)

fmt.Println(myform.Tags)

@Bloomca Does @EtienneR comment resolve your problem?

@appleboy yes, thanks a lot! Sorry for not closing it immediately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

windweller picture windweller  Â·  20Comments

nithinmohan picture nithinmohan  Â·  24Comments

colortell picture colortell  Â·  19Comments

elliotlings picture elliotlings  Â·  29Comments

mattatcha picture mattatcha  Â·  29Comments