表单值在提交后为空

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

Form value empty after post

问题

我在Go语言中读取HTML表单的值时遇到了问题,这本应该很简单。不知何故,在我的Auth_login处理程序中,'user'的值始终为空,即使我填写下面的表单并按下'submit'按钮。

当通过HTTP post方法调用/login URL时调用的处理程序:

func Auth_login(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    user := r.PostFormValue("username")
    // 其他操作在这里
}

相关表单(Slim格式):

form action=/login method=post

    br
    | Name:
    br
    input#username type=text

    br
    | Password:
    br
    input#password type=password

    br
    input type=submit
    a href=/login Cancel

在尝试读取HTML表单字段时,我是否做错了什么?

英文:

I am having trouble reading a value from an HTML form in Go, which should be straight forward. For some reason, the value 'user' in my Auth_login handler is always empty, even after I fill out the form below and press the 'submit' button.

Handler that is called when the /login url is called by an HTTP post method:

func Auth_login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	user := r.PostFormValue("username")
    // Other actions here
}

Related form (in Slim format):

  form action=/login method=post

    br
    | Name:
    br
    input#username type=text

    br
    | Password:
    br
    input#password type=password

    br
    input type=submit
    a href=/login Cancel

Am I doing something wrong when trying to read the html form field?

答案1

得分: 3

我刚刚通过查看另一个网站的源代码找到了答案。如果有人遇到类似的问题,我通过在我想要在Go中解析的每个文本框中添加一个'name'值来修复它。例如,上面的Slim代码将变成这样:

  form action=/login method=post

    br
    | 名称:
    br
    input#username type=text name=username

    br
    | 密码:
    br
    input#password type=password name=password

    br
    input type=submit
    a href=/login 取消
英文:

I just figured out the answer by looking at another website's source code. In case anyone has a similar problem, I fixed it by adding a 'name' value to each of the textboxes that I wanted to parse in Go. For example, the Slim code above would become this:

  form action=/login method=post

    br
    | Name:
    br
    input#username type=text name=username

    br
    | Password:
    br
    input#password type=password name=password

    br
    input type=submit
    a href=/login Cancel

huangapple
  • 本文由 发表于 2016年12月29日 01:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/41367225.html
匿名

发表评论

匿名网友

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

确定