Browse Source

Query parameters create hidden form elements

Remove drop shadow and header on main form to make it more embeddable
tags/v1.0.0^0
Chris Smith 5 years ago
parent
commit
0e78ac577d
3 changed files with 16 additions and 11 deletions
  1. 3
    0
      README.adoc
  2. 4
    11
      form.html
  3. 9
    0
      main.go

+ 3
- 0
README.adoc View File

@@ -56,6 +56,9 @@ and `failure.html` respectively. Each is loaded as a https://golang.org/pkg/html
56 56
 can use the templating syntax described there. The form must contain the `{{ .csrfField }}` template field, which
57 57
 will automatically insert the CSRF token for the request.
58 58
 
59
+Any query parameters passed to the form will be accessible through the `{{ .params }}` template field. The
60
+default template will include all parameters as hidden fields in the form.
61
+
59 62
 If the form contains a field named `from`, it will be used in the `Reply-To` header. Otherwise, no
60 63
 special processing is applied to any field; they are all simply written out to the message body.
61 64
 No server-side validation is performed.

+ 4
- 11
form.html View File

@@ -6,7 +6,6 @@
6 6
     <style type="text/css">
7 7
         .page {
8 8
             width: 640px;
9
-            padding: 8% 0 0;
10 9
             margin: auto;
11 10
         }
12 11
 
@@ -15,10 +14,9 @@
15 14
             z-index: 1;
16 15
             background: #FFFFFF;
17 16
             max-width: 640px;
18
-            margin: 0 auto 100px;
17
+            margin: 0 auto;
19 18
             padding: 25px 45px 35px 45px;
20 19
             text-align: center;
21
-            box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
22 20
         }
23 21
 
24 22
         .form input, .form textarea {
@@ -53,21 +51,16 @@
53 51
         .form button:hover, .form button:active, .form button:focus {
54 52
             background: #43A047;
55 53
         }
56
-
57
-        h1 {
58
-            font-family: sans-serif;
59
-            font-variant: all-small-caps;
60
-            font-size: 1.4em;
61
-            margin: 0 0 0.9em 0;
62
-        }
63 54
     </style>
64 55
 </head>
65 56
 <body>
66 57
 <div class="page">
67 58
     <div class="form">
68 59
         <form action="submit" method="post">
69
-            <h1>Contact me</h1>
70 60
             {{ .csrfField }}
61
+            {{-  range $k, $v := .params }}
62
+            <input type="hidden" name="{{ $k | html }}" value="{{ $v | html }}">
63
+            {{- end }}
71 64
             <input type="text" name="name" placeholder="Your name" required>
72 65
             <input type="email" name="from" placeholder="Your e-mail address" required>
73 66
             <textarea name="message" placeholder="Your message" required></textarea>

+ 9
- 0
main.go View File

@@ -57,8 +57,17 @@ func handleForm(rw http.ResponseWriter, req *http.Request) {
57 57
 }
58 58
 
59 59
 func showForm(rw http.ResponseWriter, req *http.Request) {
60
+	params := make(map[string]string)
61
+
62
+	for k, vs := range req.URL.Query() {
63
+		if len(vs) == 1 {
64
+			params[k] = vs[0]
65
+		}
66
+	}
67
+
60 68
 	_ = formTemplate.ExecuteTemplate(rw, "form.html", map[string]interface{}{
61 69
 		csrf.TemplateTag: csrf.TemplateField(req),
70
+		"params":         params,
62 71
 	})
63 72
 }
64 73
 

Loading…
Cancel
Save