Go的r.FormValue为空。

huangapple go评论143阅读模式
英文:

Go r.FormValue is empty

问题

我有一个HTML表单,我在其中输入值并将它们插入数据库。但是当我插入它们时,数据库中的值显示为""。

这是我插入值的方式:

  1. title, author, description := r.Form("title"), r.FormValue("author"), r.FormValue("description")
  2. fmt.Println(title, author, description)
  3. rows, err := db.Query("INSERT INTO apps (title, author, description) VALUES ($1, $2, $3)",
  4. title, author, description)
  5. PanicIf(err)
  6. defer rows.Close()
  7. http.Redirect(w, r, "/myapps", http.StatusFound)
  8. db.Close()

HTML代码:

  1. <form action="/apps" method="POST">
  2. <div class="form-group">
  3. <label>名称</label>
  4. <input type="text" class="form-control" name="title" />
  5. </div>
  6. <div class="form-group">
  7. <label>作者</label>
  8. <input type="text" class="form-control" name="author" />
  9. </div>
  10. <div class="form-group">
  11. <label>描述</label>
  12. <input type="text" class="form-control" name="description" />
  13. </div>
  14. <input type="submit" value="创建" class="btn btn-success" />
  15. <a href="/myapps">
  16. <input type="button" value="返回" class="btn btn-primary" />
  17. </a>
  18. </form>

也许是formvalues出了问题?

英文:

I got an html form where I put values and insert them into the database. but when I insert them the values appear in the db like "".

This is how I insert the values:

  1. title, author, description := r.Form("title"), r.FormValue("author"), r.FormValue("description")
  2. fmt.Println(title, author, description)
  3. rows, err := db.Query("INSERT INTO apps (title, author, description) VALUES ($1, $2, $3)",
  4. title, author, description)
  5. PanicIf(err)
  6. defer rows.Close()
  7. http.Redirect(w, r, "/myapps", http.StatusFound)
  8. db.Close()

Html :

  1. <form action="/apps" method="POST">
  2. <div class="form-group">
  3. <label>Name</label>
  4. <input type="text" class="form-control" name="title" />
  5. </div>
  6. <div class="form-group">
  7. <label>Author</label>
  8. <input type="text" class="form-control" name="author" />
  9. </div>
  10. <div class="form-group">
  11. <label>Description</label>
  12. <input type="text" class="form-control" name="description" />
  13. </div>
  14. <input type="submit" value="Create" class="btn btn-success" />
  15. <a href="/myapps">
  16. <input type="button" value="Return" class="btn btn-primary" />
  17. </a>
  18. </form>

There is maybe something wrong with the formvalues?

答案1

得分: 8

在使用r.Form / r.PostForm之前,你必须先调用r.ParseForm()

从:http://golang.org/src/pkg/net/http/request.go#L168

  1. // Form包含解析后的表单数据,包括URL字段的查询参数和POST或PUT表单数据。
  2. // 只有在调用ParseForm之后,该字段才可用。
  3. // HTTP客户端忽略Form字段,而使用Body字段。
  4. Form url.Values
  5. // PostForm包含来自POST或PUT请求体参数的解析后的表单数据。
  6. // 只有在调用ParseForm之后,该字段才可用。
  7. // HTTP客户端忽略PostForm字段,而使用Body字段。
  8. PostForm url.Values
英文:

You have to call r.ParseForm() first before you could use r.Form / r.PostForm.

From: http://golang.org/src/pkg/net/http/request.go#L168

  1. // Form contains the parsed form data, including both the URL
  2. // field's query parameters and the POST or PUT form data.
  3. // This field is only available after ParseForm is called.
  4. // The HTTP client ignores Form and uses Body instead.
  5. Form url.Values
  6. // PostForm contains the parsed form data from POST or PUT
  7. // body parameters.
  8. // This field is only available after ParseForm is called.
  9. // The HTTP client ignores PostForm and uses Body instead.
  10. PostForm url.Values

huangapple
  • 本文由 发表于 2014年7月13日 21:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/24723038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定