I have a page that is dynamically generated from my database. It basically reflects all the contents of a table.
{{range .EquipmentList}}
<form method="POST" action="/cart">
<div class="row">
<div class="col-1"></div>
<div class="col-5">
<p name="test"> {{.Name}} </p>
<br>
{{.Description}}
<br>
<button id="{{.Name}}" type="submit" class="btn btn-main btn-margin" ><b>Ausleihen</b></button>
{{if .Availability}}
<input type="number" class="form-control main-input2 feld2" id="amount" name="amount" placeholder="Anzahl">
<em class="status-text" style="color:green"> verfügbar </em>
{{else}}
<input type="number" class="form-control main-input2 feld2" id="amount" name="amount" placeholder="Anzahl">
<em class="status-text"> entliehen </em>
{{end}}
</div>
<div class="col-2"></div>
<div class="col-2" style="margin-top: 30px">
<img class="float-right img-responsive bild" src="{{.ImgPath}}" alt="Kann nicht gezeigt werden">
</div>
</div>
<div class="placeholder" style="height:50px"></div>
</form>
{{end}}
In the template, all devices from the DB are listed and provided with their own Submit button.
How can I find the fitting .Name entry to the triggered submit button?
I want to pass this value to my / cart located in the controller.
func Cart(w http.ResponseWriter, r *http.Request) {
data := Data{
Name: "Cart",
Pages: []Page{
{
Title: "Meine Geräte",
Active: false,
Link: "/my-equipment",
},
{
Title: "Equipment",
Active: false,
Link: "/equipment",
},
{
Title: "Logout",
Active: false,
Link: "/logout",
},
},
}
if r.Method=="POST"{
// SAVE .Name here
tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
tmpl.ExecuteTemplate(w, "base", data)
}else{
tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
tmpl.ExecuteTemplate(w, "base", data)
}
}
Now I got it:
{{range .EquipmentList}}
<form action="/cart?id={{.ID}}" method="POST">
<a href="/cart?id={{.ID}}">{{.Name}}</a>
<button type="submit">
</button></form>
{{end}}
if iam using this code, i can use the id value with r.formValue("id") in my controller.