Problems with HTML submit buttons

Hi, I can’t seem to get the code to work with submit buttons using a template. It seems to change the text to the previous click, not the latest click, also the function seems to run twice on every click. What am I doing wrong?

The go code I am using is

Package main

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

var modeType string = ""

type PageVariables struct {
        Mode string
      }


func whatPressed(w http.ResponseWriter, r *http.Request) {
		HomePageVars := PageVariables{ 
		Mode: modeType,
    	}
		
        t, _ := template.ParseFiles("test1.html")
        t.Execute(w, HomePageVars)
        r.ParseForm()
        
        if _, exist := r.Form["normal"]; exist { modeType = "Normal Mode"}
        if _, exist := r.Form["insert"]; exist { modeType = "Insert Mode"}
        if _, exist := r.Form["edit"]; exist { modeType = "Edit Mode"}

fmt.Println("Pressed")
fmt.Println(r.Form)
    
}


func main() {
	
	    http.HandleFunc("/", whatPressed) // setting router rule
       err := http.ListenAndServe(":8080", nil) // setting listening port
           if err != nil {
           log.Fatal("ListenAndServe: ", err)
    }

}

and the template I used - test1.html - is

<table bgcolor="#ffffff" width=800>
 <tr>
  <td width="20%" align=left><form method=post><input type="hidden" name="edit" value="1"><input type="submit" value="Edit"></form></td>
  <td width="20%" align=left><form  method=post><input type="hidden" name="insert" value="1"><input type="submit" value="Insert"></form></td>
  <td width="20%" align=left><form  method=post><input type="hidden" name="normal" value="1"><input type="submit" value="Normal"></form></td>
  <td width="20%" align=left> </td>
 </tr>
</table>
<br><br><p>{{.Mode}} has been clicked on</p>

Many thanks

Hi Dunsfold, HomePageVars should be after assigning value modeType in exist checking form.that’s why you get an empty string value first, and also you executed your template before action in form. Here’s the code implementation in whatPressed function:

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

     if _, exist := r.Form["normal"]; exist { modeType = "Normal Mode" }
     if _, exist := r.Form["insert"]; exist { modeType = "Insert Mode" }
     if _, exist := r.Form["edit"]; exist { modeType = "Edit Mode" }

     HomePageVars := PageVariables{ 
	     Mode: modeType,
     }

     fmt.Println("Pressed")
     fmt.Println(r.Form)
	
     t, _ := template.ParseFiles("test1.html")
     t.Execute(w, HomePageVars)
}

That worked. Many thanks for your help.

Picsart for window free download
Can you explain how these line of code will work. So it get easy for me to understanding.
Do you have any idea so please proceed it for helping someone.
Thanks.

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache . You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.