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
 can use the templating syntax described there. The form must contain the `{{ .csrfField }}` template field, which
56
 can use the templating syntax described there. The form must contain the `{{ .csrfField }}` template field, which
57
 will automatically insert the CSRF token for the request.
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
 If the form contains a field named `from`, it will be used in the `Reply-To` header. Otherwise, no
62
 If the form contains a field named `from`, it will be used in the `Reply-To` header. Otherwise, no
60
 special processing is applied to any field; they are all simply written out to the message body.
63
 special processing is applied to any field; they are all simply written out to the message body.
61
 No server-side validation is performed.
64
 No server-side validation is performed.

+ 4
- 11
form.html View File

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

+ 9
- 0
main.go View File

57
 }
57
 }
58
 
58
 
59
 func showForm(rw http.ResponseWriter, req *http.Request) {
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
 	_ = formTemplate.ExecuteTemplate(rw, "form.html", map[string]interface{}{
68
 	_ = formTemplate.ExecuteTemplate(rw, "form.html", map[string]interface{}{
61
 		csrf.TemplateTag: csrf.TemplateField(req),
69
 		csrf.TemplateTag: csrf.TemplateField(req),
70
+		"params":         params,
62
 	})
71
 	})
63
 }
72
 }
64
 
73
 

Loading…
Cancel
Save