labstack / echox

Echo cookbook and website
https://echo.labstack.com
MIT License
407 stars 285 forks source link

get multiple checkbox #207

Closed letran3691 closed 3 years ago

letran3691 commented 3 years ago

Hello

I've issues when get multiple checked

code html

    <form method="POST" action="permiss">
        <input type="checkbox" id="vehdsdicle1" name="role" value="admin" checked>
        <label for="all">Admin</label><br>
        <input type="checkbox" id="vehicle3" name="role" value="add" checked>
        <label for="add">Add</label><br>
        <input type="checkbox" id="vehicle2" name="role" value="edit">
        <label for="edit">Edit</label><br>
        <input type="checkbox" id="vehicle3" name="role" value="read">
        <label for="read">Read</label><br>
        <input type="checkbox" id="vehicle3" name="role" value="delete">
        <label for="delete">Delete</label><br>
        <input type="submit" value="Save" />
    </form><br />

function get requests


 id := cast.ToInt64(c.QueryParam("id"))
     a := c.Request().Form["role"]

    log.Println(id)
    log.Println("role", a)

however it's return a null arry

2021/05/14 16:28:37 role []

i've try c.FormValue["role"], but it's return only first value get

aldas commented 3 years ago

Use c.FormParams() get form as map. c.FormValue("role") and c.Request().FormValue("role") returns first value

func main() {
    e := echo.New()
    e.POST("/permiss", func(c echo.Context) error {
        valueMap, _ := c.FormParams()
        log.Print(valueMap["role"])

        return c.String(http.StatusOK, "OK")
    })

    if err := e.Start(":8080"); err != nil {
        log.Fatal(err)
    }
}
curl 'http://localhost:8080/permiss' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-raw 'role=admin&role=add&role=edit' 
letran3691 commented 3 years ago

thank very much