dounuo7954 2016-08-25 15:11
浏览 64
已采纳

Golang,不从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, "Hello" (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 (
    "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) // sets router
    http.HandleFunc("/welcome", WelcomeHandler)
    err := http.ListenAndServe(":4000", nil) // set listen port
    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)
    }

}

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

<!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>
  • 写回答

1条回答 默认 最新

  • dream890110 2016-08-25 15:18
    关注

    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("FirstName")
    Secondname := r.FormValue("LastName")
    

    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: It takes too much time when using "template" package to generate a dynamic web page to client in golang

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决