@@ -212,7 +212,23 @@ INSERT INTO component(name, icon, description) VALUES
212212 ' The value entered by the user in a field named x will be accessible to the target SQL page as a variable named $x.
213213 For instance, you can create a SQL page named "create_user.sql" that would contain "INSERT INTO users(name) VALUES($name)"
214214 and a form with its action property set to "create_user.sql" that would contain a field named "name".' );
215+ INSERT INTO parameter(component, name, description_md, type, top_level, optional) SELECT ' form' , * FROM (VALUES
216+ -- top level
217+ (' enctype' , '
218+ When ``method="post"``, this specifies how the form-data should be encoded
219+ when submitting it to the server.
220+ ' , ' TEXT' , TRUE, TRUE),
221+ -- item level
222+ (' formenctype' , '
223+ When ``type`` is ``submit`` or ``image``, this specifies how the form-data
224+ should be encoded when submitting it to the server.
225+
226+ Takes precedence over any ``enctype`` set on the ``form`` element.
215227
228+ NOTE: when a ``file`` type input is present, then ``formenctype="multipart/form-data"``
229+ is automatically applied to the default validate button.
230+ ' , ' TEXT' , FALSE, TRUE)
231+ );
216232INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT ' form' , * FROM (VALUES
217233 -- top level
218234 (' method' , ' Set this to ' ' GET' ' to pass the form contents directly as URL parameters. If the user enters a value v in a field named x, submitting the form will load target.sql?x=v. If target.sql contains SELECT $x, it will display the value v.' , ' TEXT' , TRUE, TRUE),
@@ -402,6 +418,7 @@ But note that SQLPage cookies already have the `SameSite=strict` attribute by de
402418## File upload
403419
404420You can use the `file` type to allow the user to upload a file.
421+
405422The file will be uploaded to the server, and you will be able to access it using the
406423[`sqlpage.uploaded_file_path`](functions.sql?function=uploaded_file_path#function) function.
407424
@@ -411,10 +428,55 @@ Here is how you could save the uploaded file to a table in the database:
411428INSERT INTO uploaded_file(name, data) VALUES(:filename, sqlpage.uploaded_file_data_url(:filename))
412429```
413430' ,
414- json(' [{"component":"form", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
431+ json(' [{"component":"form", "enctype": "multipart/form-data", " title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"},
415432 {"name": "my_file", "type": "file", "accept": "image/png, image/jpeg", "label": "Picture", "description": "Upload a small picture", "required": true}
416433 ]' )),
417434 (' form' , '
435+ ## Form Encoding
436+
437+ You can specify the way form data should be encoded by setting the `enctype`
438+ top-level property on the form.
439+
440+ You may also specify `formenctype` on `submit` and `image` type inputs.
441+ This will take precedence over the `enctype` specified on the form and is
442+ useful in the case there are multiple `submit` buttons on the form.
443+ For example, an external site may have specific requirements on encoding type.
444+
445+ As a rule of thumb, ``multipart/form-data`` is best when fields may contain
446+ copious non-ascii characters or for binary data such as an image or a file.
447+ However, ``application/x-www-form-urlencoded`` creates less overhead when
448+ many short ascii text values are submitted.
449+ ' ,
450+ json(' [
451+ {
452+ "component": "form",
453+ "method": "post",
454+ "enctype": "multipart/form-data",
455+ "title": "Submit with different encoding types",
456+ "validate": "Submit with form encoding type",
457+ "action": "examples/handle_enctype.sql"
458+ },
459+ {"name": "data", "type": "text", "label": "Data", "required": true},
460+ {
461+ "name": "percent_encoded",
462+ "type": "submit",
463+ "label": "Submit as",
464+ "width": 4,
465+ "formaction": "examples/handle_enctype.sql",
466+ "formenctype": "application/x-www-form-urlencoded",
467+ "value": "application/x-www-form-urlencoded"
468+ },
469+ {
470+ "name": "multipart_form_data",
471+ "type": "submit",
472+ "label": "Submit as",
473+ "width": 4,
474+ "formaction": "examples/handle_enctype.sql",
475+ "formenctype": "multipart/form-data",
476+ "value": "multipart/form-data"
477+ }
478+ ]' )),
479+ (' form' , '
418480## Bulk data insertion
419481
420482You can use the `file` type to allow the user to upload a [CSV](https://en.wikipedia.org/wiki/Comma-separated_values)
0 commit comments