如何从请求对象获取提交值

时间:2015-06-17 13:49:52

标签: html go

我正在尝试使用两个不同的提交按钮进行表单POST请求。我想尝试获取提交按钮值。我如何从http.Request对象获取此信息。

HTML代码:

<form action="/save" method="POST">
<div><span>Title: </span><textarea name="title" placeholder="Link">{{printf "%s" .Title}}</textarea></div>
<div>
    <button type="submit" value="submit1">1</button>
    <button type="submit" value="submit2">2</button>
</div>
</form>

GO代码:

func saveHandler(w http.ResponseWriter, r *http.Request) {
... How do I get the submit value
title := r.FormValue("title")
fmt.Println(title)
}
func main() {
    http.HandleFunc("/", viewHandler)
    http.HandleFunc("/save", saveHandler)
    http.ListenAndServe(":8080", nil)
}

或者我需要做一些Ajax技巧,我有一个隐藏字段并在提交之前在ajax中设置它?

1 个答案:

答案 0 :(得分:1)

为您的按钮添加name属性,然后您可以区分它们。

HTML:

<form action="/save" method="POST">
<div><span>Title: </span><textarea name="title" placeholder="Link">{{printf "%s" .Title}}</textarea></div>
<div>
    <button type="submit" value="submit1" name="submit">1</button>
    <button type="submit" value="submit2" name="submit">2</button>
</div>
</form>

转到:

submit := r.FormValue("submit")  // will be "submit1" or "submit2"