douying4203 2014-07-13 13:11
浏览 70
已采纳

转到r.FormValue为空

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条回答 默认 最新

  • dqaxw44567 2014-07-13 13:18
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解