Getting form data in GO


(Hridyansh Thakur) #1

i am trying to get form data from my server but all i get printed on my screen is []

my go code is :

package main

import (

"fmt"

"io/ioutil"

"net/http"

)

func check(e error) {

if e != nil {

panic(e)

}

}

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

r.ParseForm()

//fmt.Println(r.Form)

fmt.Println(r.Form["first_name"])

}

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

hpage, err := ioutil.ReadFile("index.html")

//fmt.Println("home page req")

check(err)

fmt.Fprintf(w, "%s", hpage)

}

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

page, err := ioutil.ReadFile("student.html")

fmt.Println("student page req")

check(err)

fmt.Fprintf(w, "%s", page)

}

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

page, err := ioutil.ReadFile("teacher.html")

//fmt.Println("teacher page req")

check(err)

fmt.Fprintf(w, "%s", page)

}

func main() {

http.HandleFunc("/", greet)

http.HandleFunc("/student", student)

http.HandleFunc("/teacher", teacher)

http.HandleFunc("/submit", submit)

j := http.ListenAndServe("192.168.0.14:8080", nil)

check(j)

}

here is my HTML:

<form class="col s12" action="/submit" method="POST">
            <div class="row">
              <div class="input-field col s6">
                <input id="first_name" type="text" class="validate">
                <label for="first_name">Name of the teacher</label>
              </div>
              <div class="input-field col s6">
                <input id="last_name" type="text" class="validate">
                <label for="last_name">Subject</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12">
                <input id="studentid" type="text" class="validate">
                <label for="studentid">Student ID</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12">
                <input id="password" type="password" class="validate">
                <label for="password">Password</label>
              </div>
            </div>
           
           
            <p class="range-field ">
              <input type="range" id="K_level" min="0" max="100" />
              <label for="K_level">Knowledge level</label>
            </p>

            <p class="range-field ">
              <input type="range" id="param1" min="0" max="100" />
              <label for="param1">some parameter</label>
            </p>

            <p class="range-field ">
              <input type="range" id="param2" min="0" max="100" />
              <label for="param2">some parameter</label>
            </p>


            <button class="btn waves-effect waves-light pink" type="submit" name="action">Submit
              <i class="material-icons right">send</i>
            </button>

          </form>

(Techer Jean Sebastien) #2

Did you try to change id attributes for name attributes in your form?

https://www.w3.org/TR/html5/sec-forms.html#forms-form-submission


(Hridyansh Thakur) #3

i tried everything in page and it failed and i don’t know what is wrong i just copied tutorial


(George Calianu) #4

Instead of

r.Form["first_name"]

use

r.FormValue("first_name")

also, use name="first_name" not id="first_name" in your html code.