Golang,不从HTML传递值

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

Golang, not passing values from HTML

问题

我正在尝试构建一个简单的双Handler网络服务器。在第一个处理程序中,我从两个字段中获取值,即用户的名字和姓氏。在第二个处理程序中,我只是简单地声明了"Hello" (名字, 姓氏)

我在代码中加入了一个fmt.Println,看起来我没有正确地将第一个处理程序中的信息传回到Go编程中。出了什么问题?

package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
)

type Field struct {
	Firstname  string
	Secondname string
}

func RootHandler(w http.ResponseWriter, r *http.Request) {
	tmpl, err := template.ParseFiles("index.html")
	if err != nil {
		fmt.Println("Index Template Parse Error: ", err)
	}
	err = tmpl.Execute(w, nil)
	if err != nil {
		fmt.Println("Index Template Execution Error: ", err)
	}

}

func main() {
	http.HandleFunc("/", RootHandler) // 设置路由
	http.HandleFunc("/welcome", WelcomeHandler)
	err := http.ListenAndServe(":4000", nil) // 设置监听端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func WelcomeHandler(w http.ResponseWriter, r *http.Request) {

	Firstname := r.FormValue("Namef")
	Secondname := r.FormValue("Namel")
	fmt.Println(Firstname)

	f := new(Field)
	f.Firstname = Firstname
	f.Secondname = Secondname
	fmt.Println(*f)
	tmpl, err := template.ParseFiles("tmpl/welcome.tmpl")
	if err != nil {
		fmt.Println("Index Template Parse Error: ", err)
	}
	err = tmpl.Execute(w, &f)
	if err != nil {
		fmt.Println("Index Template Execution Error: ", err)
	}

}

这是我从中提取两个值的index.html

<!DOCTYPE html>
<html>
<body>

<form action="/welcome" method="post">
<p> Welcome! First I will need your full name: </p>
First Name: <input type="text" id="Namef" name="FirstName"><br>
Last Name: 	<input type="text" id="Namel" name="LastName" ><br>
<input type="submit" value="Next">
</form>

<p>Click the "Submit" button and the form-data will be sent to a page on the server called "demo_form.asp".</p>

</body>
</html>

请注意,这只是一个简单的示例,可能存在其他问题。如果你能提供更多的错误信息或具体的问题描述,我可以更好地帮助你。

英文:

I am trying to build a simple, two-Handler web server. In the first handler, I am pulling values from two fields, a user's first name and last name. In the second handler I am simply stating, &quot;Hello&quot; (first name, last name).

I've put an fmt.Println, and it seems that I'm not correctly pulling the info from the first Handler back into the Go programming. Where is this going wrong?

package main
import (
&quot;fmt&quot;
&quot;html/template&quot;
&quot;log&quot;
&quot;net/http&quot;
)
type Field struct {
Firstname  string
Secondname string
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles(&quot;index.html&quot;)
if err != nil {
fmt.Println(&quot;Index Template Parse Error: &quot;, err)
}
err = tmpl.Execute(w, nil)
if err != nil {
fmt.Println(&quot;Index Template Execution Error: &quot;, err)
}
}
func main() {
http.HandleFunc(&quot;/&quot;, RootHandler) // sets router
http.HandleFunc(&quot;/welcome&quot;, WelcomeHandler)
err := http.ListenAndServe(&quot;:4000&quot;, nil) // set listen port
if err != nil {
log.Fatal(&quot;ListenAndServe: &quot;, err)
}
}
func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
Firstname := r.FormValue(&quot;Namef&quot;)
Secondname := r.FormValue(&quot;Namel&quot;)
fmt.Println(Firstname)
f := new(Field)
f.Firstname = Firstname
f.Secondname = Secondname
fmt.Println(*f)
tmpl, err := template.ParseFiles(&quot;tmpl/welcome.tmpl&quot;)
if err != nil {
fmt.Println(&quot;Index Template Parse Error: &quot;, err)
}
err = tmpl.Execute(w, &amp;f)
if err != nil {
fmt.Println(&quot;Index Template Execution Error: &quot;, err)
}
}

And here is the index.html that I'm pulling the two values from:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;form action=&quot;/welcome&quot; method=&quot;post&quot;&gt;
&lt;p&gt; Welcome! First I will need your full name: &lt;/p&gt;
First Name: &lt;input type=&quot;text&quot; id=&quot;Namef&quot; name=&quot;FirstName&quot;&gt;&lt;br&gt;
Last Name: 	&lt;input type=&quot;text&quot; id=&quot;Namel&quot; name=&quot;LastName&quot; &gt;&lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;Next&quot;&gt;
&lt;/form&gt;
&lt;p&gt;Click the &quot;Submit&quot; button and the form-data will be sent to a page on the server called &quot;demo_form.asp&quot;.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 6

问题在于你尝试使用错误的名称获取表单值。

你必须使用在HTML文档中作为name属性使用的值,而不是id

Firstname := r.FormValue("FirstName")
Secondname := r.FormValue("LastName")

而且,这可能只是你的一部分练习代码,但是不要在处理程序中解析模板。解析模板是一个相对资源密集型的任务,你应该只做一次,而且由于模板对并发使用是安全的,你可以在多个goroutine(处理并发请求)中使用单个template.Template值。有关详细信息,请参阅此问题:https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p

英文:

The problem is that you try to get the form values using wrong names.

You have to use the values you used as the name attribute in the HTML document, not the id!

Firstname := r.FormValue(&quot;FirstName&quot;)
Secondname := r.FormValue(&quot;LastName&quot;)

And this may just be a practice code from your part, but never parse templates in handlers. Parsing templates is a relatively resource-intensive task, you should do that only once, and since templates are safe for concurrent use, you can use a single template.Template value from multiple goroutines (serving concurrent requests). See this question for details: https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p

huangapple
  • 本文由 发表于 2016年8月25日 23:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/39148884.html
匿名

发表评论

匿名网友

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

确定