0
0

More than 3 years have passed since last update.

Go gin でチェックボックス取得

Posted at

ginでチェックボックスを取得・表示

大したものではないですが、一応調べる必要があったので記載してみます。
参考になれば幸いです。

<div>
   <p>趣味</p>
  <input type="checkbox" value="reading" name="hobbies" />
  <label for="checkbox">読書</label>
  <input type="checkbox" value="sports" name="hobbies" />
  <label for="checkbox">運動</label>
  <input type="checkbox" value="travel" name="hobbies" />
  <label for="checkbox">旅行</label>
  <input type="checkbox" value="game" name="hobbies" />
  <label for="checkbox">ゲーム</label>
</div>

まずフォーム自体はこんな感じですね。
メインは次で

app.POST("/form", func(content *gin.Context) {
   hobbies := content.PostFormArray("hobbies")
   content.JSON(http.StatusOK, gin.H{"hobbies": hobbies})
})
PostFormArray

を使うということです。

PostForm

では、複数要素は扱えないので、こちら使いましょう。
以上です。

参照

0
0
1

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
0
0