表单值在提交后为空

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

Form value empty after post

问题

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

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

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

相关表单(Slim格式):

  1. form action=/login method=post
  2. br
  3. | Name:
  4. br
  5. input#username type=text
  6. br
  7. | Password:
  8. br
  9. input#password type=password
  10. br
  11. input type=submit
  12. 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:

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

Related form (in Slim format):

  1. form action=/login method=post
  2. br
  3. | Name:
  4. br
  5. input#username type=text
  6. br
  7. | Password:
  8. br
  9. input#password type=password
  10. br
  11. input type=submit
  12. a href=/login Cancel

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

答案1

得分: 3

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

  1. form action=/login method=post
  2. br
  3. | 名称:
  4. br
  5. input#username type=text name=username
  6. br
  7. | 密码:
  8. br
  9. input#password type=password name=password
  10. br
  11. input type=submit
  12. 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:

  1. form action=/login method=post
  2. br
  3. | Name:
  4. br
  5. input#username type=text name=username
  6. br
  7. | Password:
  8. br
  9. input#password type=password name=password
  10. br
  11. input type=submit
  12. 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:

确定