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
では、複数要素は扱えないので、こちら使いましょう。
以上です。