Getting Form Values

Hi there,

I’m trying to get form values with req.FormValue(), but it’s returning empty strings. I don’t know why. Guess I’m doing something wrong.

I’m trying to pass one of the form values to the a new template too. Here’s the code:

func Login(rw http.ResponseWriter, req *http.Request){
if req.Method == “GET”{
t, err := template.ParseFiles(“templates/login.html”)
if err != nil{
fmt.Println(“Error occured Parsing login.html”)
}
t.Execute(rw, nil)
}
req.ParseForm()
CurrentUser = User{req.FormValue(“nick”), req.FormValue(“password”)}
able := database.ULogin(CurrentUser.Nick, CurrentUser.pass)
// to print the values
log.Println(“The boolean returned from ULogin is :”)
log.Println(able)// always prints false, cause of empty form values
log.Println("Values from struct “User” is: ") // I created a struct User{Nick, pass}
log.Println(CurrentUser.Nick, CurrentUser.pass)
log.Println("Values directly from form is: ")
log.Println(req.FormValue(“nick”), req.FormValue(“password”))
if able{
fmt.Println("User verification completed succefully: Pass is ", able)
tpl, err := template.ParseFiles(“comment.html”)
if err != nil{
fmt.Println(“Something happened while parsing files”)
}
tpl.Execute(rw, CurrentUser)// &CurrentUser… if multiple templates is returned to tpl

    session, err := store.Get(req, "new_session")
    if err != nil{
        http.Error(rw, err.Error(), 500)
        return
    }
    session.Values["uNick"] = CurrentUser.Nick
    session.Save(req, rw)
}else{
    redirToLogin(rw, req)
}

}

The form values return empty strings and It in turn makes me always get false value from database.ULogin() [ULogin compares the password and Nickname ]

And also should the form action point to? (comment.html?)
What I’m trying to do with the form value is put the Nick in a html variable.

Thanks.

i guess you need to check the method POST not GET

if req.Method == "POST" {  
...

Yeah, thanks George.

I figured that out, I created a new function (with if req.Method == (POST)) and directed the form to it.

:grin:

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