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