{"id":867,"date":"2023-03-09T15:44:42","date_gmt":"2023-03-09T15:44:42","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=867"},"modified":"2023-03-13T13:22:24","modified_gmt":"2023-03-13T13:22:24","slug":"html-forms","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-1\/html-forms\/","title":{"rendered":"HTML forms"},"content":{"rendered":"\n<p>All the content so far has been about the user of the website consuming the content on the page. But what about when they need to provide information? This could be something like the items in a shopping cart, payment details or a feedback form. <\/p>\n\n\n\n<p>To do this you need to use form elements. At the top level is the &lt;form&gt; tag. Any form elements within this tag are considered part of the same form. When that form is submitted, all the values on those elements is sent off.<\/p>\n\n\n\n<p>Attributes control where a form sends the data, something is going to need to process it. The most commonly used attributes are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Action \u2013 The page to which the data is sent<\/li><li>Method \u2013 The HTTP method used to send the data such as POST or GET<\/li><\/ul>\n\n\n\n<p>GET sends the data as part of the URL. If you&#8217;ve ever seen a URL like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#047;&#047;www.example.com\/blog.php?page=123&amp;command=edit<\/code><\/pre>\n\n\n\n<p>It is sending two variables to the blog.php page. PHP is a commonly used programming language for web applications. The variables are: <strong>page<\/strong> with a value of &#8220;123&#8221; and <strong>command<\/strong> with a value of &#8220;edit&#8221;.<\/p>\n\n\n\n<p>POST sends the data in its own packet to the page so the values aren&#8217;t visible in the URL. Most forms will use POST because of this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"form_results.php\" method=\"POST\"&gt;\n...\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>The above example would send the data to a page named form_results.php using the POST method. <\/p>\n\n\n\n<p>The &lt;form&gt; can contain one or more of the following form elements:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&lt;button&gt;<\/li><li>&lt;input&gt;<\/li><li>&lt;label&gt;<\/li><li>&lt;select&gt;<\/li><li>&lt;option&gt;<\/li><li>&lt;textarea&gt;<\/li><li>&lt;optgroup&gt;<\/li><li>&lt;fieldset&gt;<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Button<\/h2>\n\n\n\n<p>The &lt;button&gt; tag defines a clickable button.<\/p>\n\n\n\n<p>Inside a &lt;button&gt; element you can put text and tags like &lt;i&gt;, &lt;b&gt;, &lt;strong&gt;, &lt;br&gt;, &lt;img&gt;, etc.<\/p>\n\n\n\n<p>Always specify the type attribute for a &lt;button&gt; element, to tell browsers what type of button it is. This attribute can be: button, reset or submit.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Edit your index.html file and we&#8217;ll add the following link to the nav menu beneath the existing Home link.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"\/forms.html\"&gt;Forms&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<p>Make a copy of your index.html file and save it as forms.html. Edit forms.html for the rest of the examples on this page<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"\/style.css\"&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;header&gt;\n      This is my website name\n    &lt;\/header&gt;\n    &lt;nav&gt;\n      &lt;a href=\"\/index.html\"&gt;Home&lt;\/a&gt;\n      &lt;a href=\"\/forms.html\"&gt;Forms&lt;\/a&gt;\n    &lt;\/nav&gt;\n    &lt;main&gt;\n      &lt;h1&gt;Forms test page&lt;\/h1&gt;\n      &lt;form&gt;\n        &lt;button type=\"button\"&gt;Click Me&lt;\/button&gt;\n      &lt;\/form&gt;\n    &lt;\/main&gt;\n    &lt;footer&gt;\n      This is the footer.\n    &lt;\/footer&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Input<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The &lt;input&gt; tag specifies an input field where the user can enter data.<\/p>\n\n\n\n<p>The &lt;input&gt; element is the most important form element.<\/p>\n\n\n\n<p>The &lt;input&gt; element can be displayed in several ways, depending on the type attribute.<\/p>\n\n\n\n<p>The different input types are:<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\"><li>&lt;input type=\u201dbutton\u201d&gt;<\/li><li>&lt;input type=\u201dcheckbox\u201d&gt;<\/li><li>&lt;input type=\u201dcolor\u201d&gt;<\/li><li>&lt;input type=\u201ddate\u201d&gt;<\/li><li>&lt;input type=\u201ddatetime-local\u201d&gt;<\/li><li>&lt;input type=\u201demail\u201d&gt;<\/li><li>&lt;input type=\u201dfile\u201d&gt;<\/li><li>&lt;input type=\u201dhidden\u201d&gt;<\/li><li>&lt;input type=\u201dimage\u201d&gt;<\/li><li>&lt;input type=\u201dmonth\u201d&gt;<\/li><li>&lt;input type=\u201dnumber\u201d&gt;<\/li><li>&lt;input type=\u201dpassword\u201d&gt;<\/li><li>&lt;input type=\u201dradio\u201d&gt;<\/li><li>&lt;input type=\u201drange\u201d&gt;<\/li><li>&lt;input type=\u201dreset\u201d&gt;<\/li><li>&lt;input type=\u201dsearch\u201d&gt;<\/li><li>&lt;input type=\u201dsubmit\u201d&gt;<\/li><li>&lt;input type=\u201dtel\u201d&gt;<\/li><li>&lt;input type=\u201dtext\u201d&gt;(default value)<\/li><li>&lt;input type=\u201dtime\u201d&gt;<\/li><li>&lt;input type=\u201durl\u201d&gt;<\/li><li>&lt;input type=\u201dweek\u201d&gt;<\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">&lt;input&gt; attributes<\/h3>\n\n\n\n<p>Inputs can also have attributes to limit the options available to them. For example a checkbox can be marked as checked.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"checkbox\" name=\"confirm\" value=\"1\" checked&gt;<\/code><\/pre>\n\n\n\n<p>Inputs could be set to required so a value has to be entered.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" name=\"first_name\" required&gt;<\/code><\/pre>\n\n\n\n<p>They can have&nbsp; minimum and maximum values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" name=\"age\" required min=\"0\" max=\"120\"&gt;<\/code><\/pre>\n\n\n\n<p>Or minimum and maximum length of characters entered<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" name=\"first_name\" required maxlength=\"100\"&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Label<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The &lt;label&gt; tag defines a label for several elements.<\/p>\n\n\n\n<p>Proper use of labels with the elements to the right will benefit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Screen reader users \u2013 The reader will read out load the label when the user is focused on the element<\/li><li>Users who have difficulty clicking on very small regions such as checkboxes. The user can now click on the label text and it will behave as if they clicked on the form element.<\/li><\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\"><li>&lt;input type=\u201dcheckbox\u201d&gt;<\/li><li>&lt;input type=\u201dcolor\u201d&gt;<\/li><li>&lt;input type=\u201ddate\u201d&gt;<\/li><li>&lt;input type=\u201ddatetime-local\u201d&gt;<\/li><li>&lt;input type=\u201demail\u201d&gt;<\/li><li>&lt;input type=\u201dfile\u201d&gt;<\/li><li>&lt;input type=\u201dmonth\u201d&gt;<\/li><li>&lt;input type=\u201dnumber\u201d&gt;<\/li><li>&lt;input type=\u201dpassword\u201d&gt;<\/li><li>&lt;input type=\u201dradio\u201d&gt;<\/li><li>&lt;input type=\u201drange\u201d&gt;<\/li><li>&lt;input type=\u201dsearch\u201d&gt;<\/li><li>&lt;input type=\u201dtel\u201d&gt;<\/li><li>&lt;input type=\u201dtext\u201d&gt;<\/li><li>&lt;input type=\u201dtime\u201d&gt;<\/li><li>&lt;input type=\u201durl\u201d&gt;<\/li><li>&lt;input type=\u201dweek\u201d&gt;<\/li><li>&lt;meter&gt;<\/li><li>&lt;progress&gt;<\/li><li>&lt;select&gt;<\/li><li>&lt;textarea&gt;<\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"\/style.css\"&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;header&gt;\n      This is my website name\n    &lt;\/header&gt;\n    &lt;nav&gt;\n      &lt;a href=\"\/index.html\"&gt;Home&lt;\/a&gt;\n      &lt;a href=\"\/forms.html\"&gt;Forms&lt;\/a&gt;\n    &lt;\/nav&gt;\n    &lt;main&gt;\n      &lt;h1&gt;Forms test page&lt;\/h1&gt;\n      &lt;form&gt;\n        &lt;label for=\"full_name\"&gt;Name: &lt;\/label&gt;\n        &lt;input type=\"text\" name=\"full_name\" id=\"full_name\"&gt;&lt;br&gt;\n        &lt;button type=\"button\"&gt;Click Me&lt;\/button&gt;\n      &lt;\/form&gt;\n    &lt;\/main&gt;\n    &lt;footer&gt;\n      This is the footer.\n    &lt;\/footer&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Select and option<\/h2>\n\n\n\n<p>The &lt;select&gt; element is used to create a drop-down list.<\/p>\n\n\n\n<p>The &lt;option&gt; tag defines an option in a select list.<\/p>\n\n\n\n<p>The &lt;select&gt; element is most often used in a form, to collect user input.<\/p>\n\n\n\n<p>The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).<\/p>\n\n\n\n<p>The id attribute is needed to associate the drop-down list with a label.<\/p>\n\n\n\n<p>The &lt;option&gt; tags inside the &lt;select&gt; element define the available options in the drop-down list.<\/p>\n\n\n\n<p>The &lt;option&gt; tag can be used without any attributes, but you usually need the value attribute, which indicates what is sent to the server on form submission.<\/p>\n\n\n\n<p>Always add the &lt;label&gt; tag for best accessibility practices.<\/p>\n\n\n\n<p>You can use the selected attribute on an option to automatically select one of the options.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Textarea<\/h2>\n\n\n\n<p>The &lt;textarea&gt; tag defines a multi-line text input control.<\/p>\n\n\n\n<p>The &lt;textarea&gt; element is often used in a form, to collect user inputs like comments or reviews.<\/p>\n\n\n\n<p>A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier).<\/p>\n\n\n\n<p>The size of a text area is specified by the cols and rows attributes (or with CSS).<\/p>\n\n\n\n<p>The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the text area will be submitted).<\/p>\n\n\n\n<p>The id attribute is needed to associate the text area with a label.<\/p>\n\n\n\n<p>Tip: Always add the &lt;label&gt; tag for best accessibility practices.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"\/style.css\"&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;header&gt;\n      This is my website name\n    &lt;\/header&gt;\n    &lt;nav&gt;\n      &lt;a href=\"\/index.html\"&gt;Home&lt;\/a&gt;\n      &lt;a href=\"\/forms.html\"&gt;Forms&lt;\/a&gt;\n    &lt;\/nav&gt;\n    &lt;main&gt;\n      &lt;h1&gt;Forms test page&lt;\/h1&gt;\n      &lt;form&gt;\n        &lt;label for=\"full_name\"&gt;Name: &lt;\/label&gt;\n        &lt;input type=\"text\" name=\"full_name\" id=\"full_name\"&gt;&lt;br&gt;\n        &lt;label for=\"salutation\"&gt;Title: &lt;\/title&gt;\n        &lt;select id=\"salutation\" name=\"salutation\"&gt;\n          &lt;option value=\"0\"&gt;Mr&lt;\/option&gt;\n          &lt;option value=\"1\"&gt;Ms&lt;\/option&gt;\n          &lt;option value=\"2\"&gt;Mrs&lt;\/option&gt;\n          &lt;option value=\"3\"&gt;Miss&lt;\/option&gt;\n          &lt;option value=\"4\"&gt;Dr&lt;\/option&gt;\n        &lt;\/select&gt;&lt;br&gt;\n        &lt;label for=\"comments\"&gt;Comments: &lt;\/label&gt;\n        &lt;textarea name=\"comments\" id=\"comments\"&gt;&lt;\/textarea&gt;&lt;br&gt;\n        &lt;button type=\"button\"&gt;Click Me&lt;\/button&gt;\n      &lt;\/form&gt;\n    &lt;\/main&gt;\n    &lt;footer&gt;\n      This is the footer.\n    &lt;\/footer&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Fieldset and legend<\/h2>\n\n\n\n<p>The &lt;fieldset&gt; tag is used to group related elements in a form.<\/p>\n\n\n\n<p>The &lt;fieldset&gt; tag draws a box around the related elements.<\/p>\n\n\n\n<p>The &lt;legend&gt; tag is used to define a caption for the &lt;fieldset&gt; element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"\/style.css\"&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;header&gt;\n      This is my website name\n    &lt;\/header&gt;\n    &lt;nav&gt;\n      &lt;a href=\"\/index.html\"&gt;Home&lt;\/a&gt;\n      &lt;a href=\"\/forms.html\"&gt;Forms&lt;\/a&gt;\n    &lt;\/nav&gt;\n    &lt;main&gt;\n      &lt;h1&gt;Forms test page&lt;\/h1&gt;\n      &lt;form&gt;\n        &lt;fieldset&gt;\n          &lt;legend&gt;User Details&lt;\/legend&gt;\n          &lt;label for=\"full_name\"&gt;Name: &lt;\/label&gt;\n          &lt;input type=\"text\" name=\"full_name\" id=\"full_name\"&gt;&lt;br&gt;\n          &lt;label for=\"salutation\"&gt;Title: &lt;\/title&gt;\n          &lt;select id=\"salutation\" name=\"salutation\"&gt;\n            &lt;option value=\"0\"&gt;Mr&lt;\/option&gt;\n            &lt;option value=\"1\"&gt;Ms&lt;\/option&gt;\n            &lt;option value=\"2\"&gt;Mrs&lt;\/option&gt;\n            &lt;option value=\"3\"&gt;Miss&lt;\/option&gt;\n            &lt;option value=\"4\"&gt;Dr&lt;\/option&gt;\n          &lt;\/select&gt;&lt;br&gt;\n        &lt;\/fieldset&gt;\n        &lt;label for=\"comments\"&gt;Comments: &lt;\/label&gt;\n        &lt;textarea name=\"comments\" id=\"comments\"&gt;&lt;\/textarea&gt;&lt;br&gt;\n        &lt;button type=\"button\"&gt;Click Me&lt;\/button&gt;\n      &lt;\/form&gt;\n    &lt;\/main&gt;\n    &lt;footer&gt;\n      This is the footer.\n    &lt;\/footer&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"499\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13132210\/html8.png\" alt=\"Example form page\" class=\"wp-image-878\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13132210\/html8.png 608w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13132210\/html8-300x246.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><figcaption>Example form page<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/\" data-type=\"page\" data-id=\"52\">End of the week<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the content so far has been about the user of the website consuming the content on the page. But what about when they need to provide information? This could be something like the items in a shopping cart, payment details or a feedback form. To do this you need to use form elements. At&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-1\/html-forms\/\">Continue reading <span class=\"screen-reader-text\">HTML forms<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":54,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-867","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=867"}],"version-history":[{"count":7,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/867\/revisions"}],"predecessor-version":[{"id":879,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/867\/revisions\/879"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/54"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}