Thursday, April 25, 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 perform appears to run twice on each click on. What am I doing incorrect?

The go code I’m utilizing is

Bundle most important

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 most important() {
	
	    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><kind methodology=publish><enter kind="hidden" title="edit" worth="1"><enter kind="submit" worth="Edit"></kind></td>
  <td width="20%" align=left><kind  methodology=publish><enter kind="hidden" title="insert" worth="1"><enter kind="submit" worth="Insert"></kind></td>
  <td width="20%" align=left><kind  methodology=publish><enter kind="hidden" title="regular" worth="1"><enter kind="submit" worth="Regular"></kind></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 kind.that’s why you get an empty string worth first, and likewise you executed your template earlier than motion in kind. Right here’s the code implementation in whatPressed perform:

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 on your assist.

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

Picsart for window free download
Do you have got any concept so please proceed it for serving to somebody.
Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments