Get Form Data (Password field)

Hello, I’m trying to get form values, so I did what’s in the image. I get an error that I cannot resolve a password field data to a string on my server (backend). What data type is suitable for this. Please look at the screenshot and suggest other ways if this is a wrong practice…

Thanks…

You are not checking the error returned from request.ParseForm on line 43. You are also not handling the error on line 40.

1 Like

Yeah, I have tried the handling methods I know. Can you please help me out with a code snippet?

Thanks

there is if request.Method == "GET. = and == are different things. also see here how to handle errors.

Okay, thanks. I had corrected that “== and =” earlier. I got the error after taking the shot

should work with request.FormValue(“nick”) on line 45 (also for password) and without line 43.

Yeah George, I saw a sample like that and I have tried, No errors. Can I send you the code in your email, you may be able to help. I’m trying to render different pages, on different occasions


package main

import(
“net/http”
“html/template”
github.com/gorilla/mux
“log”
“fmt”
)

type Person struct{
nick string
password string
}

func main() {
serveWeb()
}

func serveWeb(){
myMux := mux.NewRouter()
myMux.HandleFunc("/", serveHome)
myMux.HandleFunc("/login", serveLoginPage)
myMux.HandleFunc("/signup", serveSignUp)

http.Handle("/", myMux)
err := http.ListenAndServe(":8080", nil)
if err != nil{
log.Fatal("Listen and Serve: ", err)
}
}

func serveHome(writer http.ResponseWriter, request *http.Request){
t, _ := template.ParseFiles(“index.html”)
t.Execute(writer, nil)
}

func serveSignUp(writer http.ResponseWriter, request *http.Request){
if request.Method == “GET”{
t, _:= template.ParseFiles(“signup.html”)
t.Execute(writer, nil)
}else{
request.ParseForm()

p := Person{request.FormValue("nick"), request.FormValue("password")}
fmt.Println(p.nick, p.password)

}

}

func serveLoginPage(writer http.ResponseWriter, request *http.Request){
t, _ := template.ParseFiles(“login.html”)
t.Execute(writer, nil)
}

send me the code but also the templates.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.