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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器