I would like to show client another page after data has been collected from previous page. But I have trouble redirect the new URL on the server side. Here is my logic:
And I am stuck on step 3 (here is an example of the flow):
type Stuff struct{ List []string } func checkcheck(w http.ResponseWriter, r *http.Request) { sinfo := Stuff{ List: some_slice } t, err := template.New("").Parse(tpl_ds) checkErr(err) err = r.ParseForm() checkErr(err) err = t.Execute(w, sinfo) checkErr(err) if r.Method == "POST" { saveChoice(r.Form["choices"]) /* step 3: make user open another URL */ } }
And here is the template:
<html> <script> $(function () { $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', data: $('form').serialize(), }); }); }); </script> <body> <form method="POST"> {{range .List}} <input type="checkbox" name="choices" value="{{.}}"> <span>{{.}}</span><br> {{end}} <input type="submit" value="Submit"> </form> </body> </html>
May I know how I can redirect to a new page?
p.s. If I put URL on button, then server is not going to run saveChoice()
What is URL redirect? URL redirect (URL forwarding) allows you to forward your domain visitors to any URL of your choice (to a new domain or a different website). You can set 301 (Permanent), 302 (Unmasked), and Masked (URL Frame) redirects for the domain names pointed to BasicDNS, PremiumDNS or FreeDNS.
Type "cache:sitename.com" in the address bar of Chrome and press "Enter" where "sitename" is the URL that is generating the redirect. This will show you a cached version of the site on which you can use the Inspect Element pane to find and capture the redirect URL.
When you redirect a URL, you're simply forwarding it to another address on the same, or different domain. You can set up a redirect that sends visitors to your new domain name when they'll try to access a URL that belonged to your old domain.
The http status 303 is the appropriate response here. So redirect the request with it.
if r.Method == "POST" { saveChoice(r.Form["choices"]) http.Redirect(w, r, newUrl, http.StatusSeeOther) }
And if your newUrl
is supposed to return a proper html page to the browser, you don't need to use ajax. Use an html form.
<form action="/postHandler" method="post"> {{range .List}} <input type="checkbox" name="choices" value="{{.}}"> <span>{{.}}</span><br> {{end}} <input type="submit" value="Submit"> </form>
Notice action
of the form is defined as /postHandler
. Put the name of the endpoint that runs your saveChoice
function there.
So to avoid http: multiple response.WriteHeader calls
error you get use this code.
func checkcheck(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { sinfo := Stuff{ List: some_slice } t, err := template.New("").Parse(tpl_ds) checkErr(err) err = r.ParseForm() checkErr(err) err = t.Execute(w, sinfo) checkErr(err) } if r.Method == "POST" { saveChoice(r.Form["choices"]) http.Redirect(w, r, newUrl, http.StatusSeeOther) } }
Otherwise, the server attempts to render both the form and the redirected url which will result in mutiple calls to the response writer.
package main import ( "net/http" "html/template" ) type data struct { List string } func main() { http.HandleFunc("/", check) } func check(w http.ResponseWriter, r * http.Request) { if r.Method == "GET" { sinfo: = data { List: "Here is a list of the files Located with in", } var tpl_ds = "index.html" //t, err := template.New("").Parse(tpl_ds) t: = template.Must(template.ParseFiles(tpl_ds)) r.ParseForm() t.Execute(w, sinfo) } if r.Method == "POST" { saveChoice(r.Form["choices"]) http.Redirect(w, r, newUrl, http.StatusSeeOther) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With