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!
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.
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.
Witch is more simple for get selected inputs.