Original post

Hey, this is my first post in this sub and I’m not sure if support questions are allowed, but here we .

So I have this HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<title>User Input</title>
</head>
<style>
h1 {
color: grey;
text-align: center;
}
form {
text-align: center;
}
</style>
<body>


Add An Item To Your Shopping Cart



Item:





https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js

document.getElementById('submit_button').onclick = function(){
alert(You’ve added an item to your shopping list!`); }; </body> </html>`

That essentially displays a little text box and allows people to “submit items”. Which is invoked with this go script:

package main
import (
"net/http"
"html/template"
"fmt"
)
var tpl *template.Template
func init(){
tpl = template.Must(template.ParseGlob("main.html"))
}

func main(){
http.HandleFunc("/", index)
http.HandleFunc("/adding", processor)
http.ListenAndServe(":8000", nil)
}
func index(w http.ResponseWriter, r *http.Request){
tpl.ExecuteTemplate(w, "main.html", nil)
}
func processor(w http.ResponseWriter, r *http.Request){
if r.Method != "GET"{
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
item := r.FormValue("item")
d := struct{
Item string
}{
Item: item,
}
fmt.Println(string(d.Item))
tpl.ExecuteTemplate(w, "main.html", d)
}

And so what I’m trying to do is save the HTML input to d’s field, but when I printed it, each time an Item was submitted through the frontend, it just outputted “Add”. What’s wrong here; how can I make it store/output an added item?

submitted by /u/DGKSK8LIFE
[link] [comments]