getting funcs to execute from an HTML template ( button )

1,559 views
Skip to first unread message

Dave Rose

unread,
Oct 16, 2015, 5:54:13 PM10/16/15
to golang-nuts
I've read a lot of posts about getting data from webapps . I am having a bit of trouble getting my button to execute a function which I think should work


The code runs against a DB and presents a list of devices. The template works and displays the page as I would like it, but what I need to do it have the button click run a function that takes {{.SystemName}} and posts that value to a function called sbcs . When I click my buttons the Printn() debugging never gets executed. 

I know there is a template.Funcs function, but it only takes string values and I am returning a struct .

Thanks in advance.
Dave

Nodir Turakulov

unread,
Oct 16, 2015, 6:01:45 PM10/16/15
to Dave Rose, golang-nuts
action attr in form elem should be "/sbcs", not "http://sbcs". 
"type" attr in button elem specified twice.
better to wrap {{.SystemName}} in value attr of button elem with quotes: value="{{.SystemName}}"

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Rose

unread,
Oct 16, 2015, 9:14:31 PM10/16/15
to golang-nuts, dave...@gmail.com
Thanks Nodir, 

I made the suggested changes but the function still doesn't get called :( . Any other ideas?

func main() {

        http.HandleFunc("/sbcs", sbcs)

        http.HandleFunc("/systemnames", systemnames)

        http.HandleFunc("/home", home)

        log.Fatal(http.ListenAndServe("99.119.228.55:8080", nil))


}


The second part of func systemnames ( after else ) doesn't execute and I know it's probably wrong, but my page with all systems does get returned. 

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

        if r.Method == "GET" {

                fmt.Println("Getting ready to serve all systems")

                var sbcNames, _ = sbcutilities.GetAllSystemNames()

                var err error = apopPage.Execute(w, Page{

                        Title:   "System Names",

                        Content: sbcNames,

                })

                if err != nil {

                        fmt.Println(err)

                }

        } else {

                fmt.Println("Getting ready to print the system details")

                r.ParseForm()

                systemname := fmt.Sprint(r.Form["systemname"])

                var sbcs, _ = sbcutilities.GetSystemDetails(systemname)

                var err error = sbcPage.Execute(w, Page{

                        Title:   "SBC Details",

                        Content: sbcs,

                })

                if err != nil {

                        panic(err)

                }

        }



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

        fmt.Println("Inside SBCS func()")

        r.ParseForm()

        name := r.FormValue("systemName")

        var sbcs, _ = sbcutilities.GetSystemDetails(name)

        fmt.Println(sbcs)

        var err error = sbcPage.Execute(w, Page{

                Title:   "SBC Detail",

                Content: sbcs,

        })

        if err != nil {

                panic(err)

        }

}




{{define "content"}}

<div class="w3-responsive">


 <div class="w3-col">

        <table class="table table-hover">

        <thread>

        <th>System Name</th>

        </thread>

<tbody>

   {{range . }}

<tr>

<td>{{.SystemName}}<form action="/sbcs" method="post" name="systemName" id="systemName" > <button type="button" type="submit" value="{{.SystemName}}" class="btn btn-primary">Show Details</button></form></td>

</tr>

     {{end}}

</tbody>

   </table>

   </div>

</div>

</div>

{{end}}

Nodir Turakulov

unread,
Oct 17, 2015, 2:38:16 AM10/17/15
to Dave Rose, golang-nuts

systemnames is not called on button click, because it is bound to /systemnames while the form sends a request to /sbcs.

I recommend watching network activity in chrome dev tools. You will see where requests are sent to and what values are sent.

Reply all
Reply to author
Forward
0 new messages