英文:
Go r.FormValue is empty
问题
我有一个HTML表单,我在其中输入值并将它们插入数据库。但是当我插入它们时,数据库中的值显示为""。
这是我插入值的方式:
title, author, description := r.Form("title"), r.FormValue("author"), r.FormValue("description")
fmt.Println(title, author, description)
rows, err := db.Query("INSERT INTO apps (title, author, description) VALUES ($1, $2, $3)",
title, author, description)
PanicIf(err)
defer rows.Close()
http.Redirect(w, r, "/myapps", http.StatusFound)
db.Close()
HTML代码:
<form action="/apps" method="POST">
<div class="form-group">
<label>名称</label>
<input type="text" class="form-control" name="title" />
</div>
<div class="form-group">
<label>作者</label>
<input type="text" class="form-control" name="author" />
</div>
<div class="form-group">
<label>描述</label>
<input type="text" class="form-control" name="description" />
</div>
<input type="submit" value="创建" class="btn btn-success" />
<a href="/myapps">
<input type="button" value="返回" class="btn btn-primary" />
</a>
</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:
title, author, description := r.Form("title"), r.FormValue("author"), r.FormValue("description")
fmt.Println(title, author, description)
rows, err := db.Query("INSERT INTO apps (title, author, description) VALUES ($1, $2, $3)",
title, author, description)
PanicIf(err)
defer rows.Close()
http.Redirect(w, r, "/myapps", http.StatusFound)
db.Close()
Html :
<form action="/apps" method="POST">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="title" />
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" name="author" />
</div>
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" name="description" />
</div>
<input type="submit" value="Create" class="btn btn-success" />
<a href="/myapps">
<input type="button" value="Return" class="btn btn-primary" />
</a>
</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
// Form包含解析后的表单数据,包括URL字段的查询参数和POST或PUT表单数据。
// 只有在调用ParseForm之后,该字段才可用。
// HTTP客户端忽略Form字段,而使用Body字段。
Form url.Values
// PostForm包含来自POST或PUT请求体参数的解析后的表单数据。
// 只有在调用ParseForm之后,该字段才可用。
// HTTP客户端忽略PostForm字段,而使用Body字段。
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
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values
// PostForm contains the parsed form data from POST or PUT
// body parameters.
// This field is only available after ParseForm is called.
// The HTTP client ignores PostForm and uses Body instead.
PostForm url.Values
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论