Sunday, May 19, 2024
HomeGolangIssues with HTML submit buttons - Code Assessment

Issues with HTML submit buttons – Code Assessment


Hello, I can’t appear to get the code to work with submit buttons utilizing a template. It appears to change the textual content to the earlier click on, not the newest click on, additionally the operate appears to run twice on each click on. What am I doing unsuitable?

The go code I’m utilizing is

Bundle foremost

import (
	"textual content/template"
	"log"
	"internet/http"
	"fmt"
)

var modeType string = ""

kind 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.Type["normal"]; exist { modeType = "Regular Mode"}
        if _, exist := r.Type["insert"]; exist { modeType = "Insert Mode"}
        if _, exist := r.Type["edit"]; exist { modeType = "Edit Mode"}

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


func foremost() {
	
	    http.HandleFunc("https://discussion board.golangbridge.org/", whatPressed) // setting router rule
       err := http.ListenAndServe(":8080", nil) // setting listening port
           if err != nil {
           log.Deadly("ListenAndServe: ", err)
    }

}

and the template I used – test1.html – is

<desk bgcolor="#ffffff" width=800>
 <tr>
  <td width="20%" align=left><type technique=publish><enter kind="hidden" identify="edit" worth="1"><enter kind="submit" worth="Edit"></type></td>
  <td width="20%" align=left><type  technique=publish><enter kind="hidden" identify="insert" worth="1"><enter kind="submit" worth="Insert"></type></td>
  <td width="20%" align=left><type  technique=publish><enter kind="hidden" identify="regular" worth="1"><enter kind="submit" worth="Regular"></type></td>
  <td width="20%" align=left> </td>
 </tr>
</desk>
<br><br><p>{{.Mode}} has been clicked on</p>

Many thanks

Hello Dunsfold, HomePageVars ought to be after assigning worth modeType in exist checking type.that’s why you get an empty string worth first, and likewise you executed your template earlier than motion in type. Right here’s the code implementation in whatPressed operate:

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

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

     HomePageVars := PageVariables{ 
	     Mode: modeType,
     }

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

That labored. Many thanks in your assist.

Are you able to clarify how these line of code will work. So it get simple for me to understanding.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments