从HTML解析输入< form>在Golang [英] Parse input from HTML <form> in Golang

查看:123
本文介绍了从HTML解析输入< form>在Golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 Goji框架

 







$ b $ g $ zenazn / goji
github.com/zenazn/goji/web


func hello(c web.C,w http.ResponseWriter,r * http.Request ){
fmt.Fprintf(w,Hello,%s!,c.URLParams [name])
}

func main(){
goji.Get(/ hello /:name,hello)
goji.Serve()
}

我希望有人能帮我做的是弄清楚HTML表单何时提交将数据发送到Golang代码。



因此,如果有一个带有name属性的输入字段,并且其值是name,并且用户在其中键入名称并提交,则在表单提交页面上,Golang代码将打印出hello,name。



以下是我可以想到的:

 包主要

导入(
fmt
净/ http

github.com/zenazn/goji
github.com/zenazn/goji/web


func hello(c web.C,w http.ResponseWriter,r * http.Request){
name:= r.PostFormValue(name)
fmt.Fprintf(w,Hello,%s!,name)
}

func main(){
goji.Handle(/ hello /,hello)
goji.Serve()
}

这里是我的hello.html文件:



在主体中:

 < form action =method =get> 
< input type =textname =name/>
< / form>

如何将 hello.html 连接到 hello.go 以便Golang代码获取输入内容并返回hello,名称以表单提交的页面形式表示?



我非常感谢任何和所有的帮助! 解决方案

r.ParseForm() 。您可以获取表单值。



所以这段代码:

  func hello(c web.C,w http.ResponseWriter,r * http.Request){
name:= r.PostFormValue(name)
fmt.Fprintf(w,Hello, %s!,name)
}

应该是这样的:

  func hello(c web.C,w http.ResponseWriter,r * http.Request){

//呼叫到ParseForm使表单字段可用。
err:= r.ParseForm()
if err!= nil {
//通过记录处理错误,然后返回
}

名称:= r.PostFormValue(name)
fmt.Fprintf(w,Hello,%s!,name)
}

编辑:我应该注意,这是一个让我在学习 net / http


I got something running with the Goji framework:

package main

import (
        "fmt"
        "net/http"

        "github.com/zenazn/goji"
        "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}

func main() {
        goji.Get("/hello/:name", hello)
        goji.Serve()
}

What I was hoping someone could help me do is figure out how when an HTML form is submitted to send that data to Golang code.

So if there is an input field with the name attribute and the value of that is name and the user types a name in there and submits, then on the form submitted page the Golang code will print hello, name.

Here is what I could come up with:

package main

import(
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

func main(){
    goji.Handle("/hello/", hello)
    goji.Serve()
}

and here is my hello.html file:

in the body:

<form action="" method="get">
    <input type="text" name="name" />
</form>

How do I connect hello.html to hello.go so that the Golang code gets what is in the input and returns hello, name in the form submitted page?

I'd greatly appreciate any and all help!

解决方案

In order to read html form values you have to first call r.ParseForm(). The you can get at the form values.

So this code:

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

Should be this:

func hello(c web.C, w http.ResponseWriter, r *http.Request){

    //Call to ParseForm makes form fields available.
    err := r.ParseForm()
    if err != nil {
        // Handle error here via logging and then return            
    }

    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

Edit: I should note that this was a point that tripped me up when learning the net/http package

这篇关于从HTML解析输入&lt; form&gt;在Golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆