Form
Content-Type Requested: application/x-www-form-urlencoded

Bind

1
package main
2
3
import "github.com/kataras/iris/v12"
4
5
func main() {
6
app := iris.New()
7
app.RegisterView(iris.HTML("./templates", ".html"))
8
9
app.Get("/", showForm)
10
app.Post("/", handleForm)
11
12
app.Listen(":8080")
13
}
14
15
func showForm(ctx iris.Context) {
16
ctx.View("form.html")
17
}
18
19
type formExample struct {
20
Colors []string `form:"colors[]"` // or just "colors".
21
}
22
23
func handleForm(ctx iris.Context) {
24
var form formExample
25
err := ctx.ReadForm(&form)
26
if err != nil {
27
ctx.StopWithError(iris.StatusBadRequest, err)
28
return
29
}
30
31
ctx.JSON(iris.Map{"Colors": form.Colors})
32
}
Copied!
templates/form.html
1
<form action="/" method="POST">
2
<p>Check one or more colors</p>
3
4
<label for="red">Red</label>
5
<!-- name can be "colors" too -->
6
<input type="checkbox" name="colors[]" value="red" id="red">
7
<label for="green">Green</label>
8
<input type="checkbox" name="colors[]" value="green" id="green">
9
<label for="blue">Blue</label>
10
<input type="checkbox" name="colors[]" value="blue" id="blue">
11
<input type="submit">
12
</form>
Copied!
Result
1
{
2
"Colors": [
3
"red",
4
"green",
5
"blue"
6
]
7
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link
Contents
Bind