LoginSignup
11
7

More than 5 years have passed since last update.

Golang+ginでフォームからPOSTされた情報を取得する話

Posted at

メモ書き的な

server.go

func init() {
    loadTemplates()
}

func main() {

    server = gin.Default()

    server.Static("/public/css/", "./public/css")
    server.Static("/public/js/", "./public/js/")
    server.Static("/public/fonts/", "./public/fonts/")
    server.Static("/public/img/", "./public/img/")

    //様々なルーティングの省略

    server.POST("/signuped", SignupedRoute)

    server.Run(":3000")
}


func loadTemplates(){
    baseTemplate := "templates/layout/_base.html"
    templates = make(map[string]*template.Template)

    templates["signuped"] = template.Must(template.ParseFiles(baseTemplate, "templates/account/signuped.html",))

}


func SignupedRoute(g *gin.Context) {
    g.Request.ParseForm()
    fmt.Println(g.Request.Form["id"])

}

この状態で

signupHTML.html
<form  method="POST" action="./signuped">
        <h2 class="form-signin-heading">会員登録</h2>
        ユーザ名(20字以内)
        <input name="id" type="text" >        
        メールアドレス
        <input name="name" type="text" >

        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
    </form>

と定義したHTMLからsignupedに対してPOSTをすると
とりあえず、name="id"に入力した値が標準出力される。

11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7