action attribute directs where the form should go. As a convention that HTTP verb POST is capitalized. It works as well if written as method="post" all lower case.
<formaction="/example.html"method="POST"></form>
method="GET" vs method="POST" specifies how form data is submitted to server. When the method is GET, all form data is encoded into the URL, appended to the action URL as query string parameters. With POST, form data appears within the message body of the HTTP request.
"GET" form data is encoded and retrieve a specific resource.
Send info from my form to "story.html" location using GET request.
<formaction="story.html"method="GET"></form>
"POST" form data appears within the message body of the HTTP request and create a new resource.
Every <input>must have name attribute for server to receive its data. name attribute for input value are submitted together as a pair to track which input value goes with name. Think of it as name: value pair.
To associate <label> to <input> in form. <label>for="username" need to match up with <input>id="username". This creates <label> and <input> association.
Add id attribute with value "meal" to <input>
Assign for attribute with value "meal" of <label>. id and for value must match for association to work.
<formaction="/example.html"method="POST"><labelfor="meal">What do you want to eat?</label><inputtype="text"name="food"id="meal"></form>
To show default text to appear in a field, include it between the opening and closing <textarea>
</h2>
<p></p>
<div class="highlight"><pre class="highlight html"><code> <span class="nt"><textarea></span>
Click here to write
<span class="nt"></textarea></span>
</code></pre></div>
<p></p>
<p>Display Text area larger by setting dimension (rows, cols).<br>
</p>
<div class="highlight"><pre class="highlight html"><code> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"message"</span><span class="nt">></span>Meaningful Message:<span class="nt"></label></span>
<span class="nt"><textarea</span> <span class="na">name=</span><span class="s">"message"</span> <span class="na">id=</span><span class="s">"message"</span> <span class="na">cols=</span><span class="s">"40"</span> <span class="na">rows=</span><span class="s">"8"</span> <span class="na">required</span><span class="nt">></span>Click here to write<span class="nt"></textarea></span>
<span class="nt"><br></span>
</code></pre></div>
<p></p>
<h2>
<a name="difference-between-raw-ltselectgt-endraw-vs-raw-ltdatalistgt-endraw-is-raw-ltdatalistgt-endraw-is-more-flexible-user-may-input-whatever-value-or-one-of-the-options-in-drop-list-for-this-case-the-value-of-the-raw-ltinputgt-endraw-s-name-and-the-value-of-the-option-selected-from-dropdown-list-or-what-the-user-typed-in-is-sent-as-a-pair" href="#difference-between-raw-ltselectgt-endraw-vs-raw-ltdatalistgt-endraw-is-raw-ltdatalistgt-endraw-is-more-flexible-user-may-input-whatever-value-or-one-of-the-options-in-drop-list-for-this-case-the-value-of-the-raw-ltinputgt-endraw-s-name-and-the-value-of-the-option-selected-from-dropdown-list-or-what-the-user-typed-in-is-sent-as-a-pair" class="anchor">
</a>
Difference between <code><select></code> vs. <code><datalist></code> is <code><datalist></code> is more flexible. User may input whatever value or one of the options in drop list. For this case, the value of the <code><input></code>‘s name and the value of the option selected from drop-down list, or what the user typed in, is sent as a pair.
</h2>
<p>Important Note: In order for <code><datalist></code> to work with <code><input></code>, <code><input></code> <strong>must</strong> associate itself by adding <code>list</code> attribute with <code><datalist></code>. <code>id="cities"</code> of <code><datalist></code> matches <code>list="cities"</code>. </p>
<p>Remember to add <code><label></code>, <code><input></code> when creating <code><datalist></code>.<br>
</p>
<div class="highlight"><pre class="highlight html"><code><span class="nt"><form></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"city"</span><span class="nt">></span>Ideal city to visit?<span class="nt"></label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">list=</span><span class="s">"cities"</span> <span class="na">id=</span><span class="s">"city"</span> <span class="na">name=</span><span class="s">"city"</span><span class="nt">></span>
<span class="nt"><datalist</span> <span class="na">id=</span><span class="s">"cities"</span><span class="nt">></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"New York City"</span><span class="nt">></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"Tokyo"</span><span class="nt">></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"Barcelona"</span><span class="nt">></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"Mexico City"</span><span class="nt">></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"Melbourne"</span><span class="nt">></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"Other"</span><span class="nt">></option></span>
<span class="nt"></datalist></span>
<span class="nt"></form></span>
</code></pre></div>
<p></p>
<p class="codepen" data-height="265" data-theme-id="dark" data-default-tab="html,result" data-user="heggy231" data-slug-hash="ZEbaoPP" style="height: 265px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="datalist form">
<span>See the Pen <a href="https://codepen.io/heggy231/pen/ZEbaoPP">
datalist form</a> by Heggyhere (<a href="https://codepen.io/heggy231">@heggy231</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
<h2>
<a name="matching-a-pattern-using-regular-expressions-a-sequence-of-characters-that-make-up-a-search-pattern-in-forms-use-raw-pattern-endraw-attribute" href="#matching-a-pattern-using-regular-expressions-a-sequence-of-characters-that-make-up-a-search-pattern-in-forms-use-raw-pattern-endraw-attribute" class="anchor">
</a>
Matching a Pattern using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a> (a sequence of characters that make up a search pattern) in forms. Use <code>pattern</code> attribute.
</h2>
<p>ex) Validate credit card number (14 - 16 digits). <code>[0-9]</code> for user to only provide numbers. <code>{14,16}</code> least 14 digits and at most 16 digits<br>
</p>
<div class="highlight"><pre class="highlight html"><code><span class="nt"><input</span> <span class="na">id=</span><span class="s">"payment"</span> <span class="na">name=</span><span class="s">"payment"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">required</span> <span class="na">pattern=</span><span class="s">"[0-9]{14,16}"</span><span class="nt">></span>
</code></pre></div>
<p></p>
<p>ex) Validate usernames to only letters and numbers (and not special characters like ! or @). <code>[a-zA-Z0-9]+</code> for only letters and numbers. <code>+</code> is <a href="https://www.rexegg.com/regex-quickstart.html#morequants">quantifier</a> that matches pattern one or more.<br>
</p>
<div class="highlight"><pre class="highlight javascript"><code><span class="dl">"</span><span class="s2">abc123AB</span><span class="dl">"</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="sr">/</span><span class="se">[</span><span class="sr">a-zA-Z0-9</span><span class="se">]</span><span class="sr">+/</span><span class="p">);</span>
<span class="c1">// => returns all characters that matches "abc123AB" because of `+` greedy quantifier</span>
</code></pre></div>
<p></p>
<p></p>
<div class="highlight"><pre class="highlight html"><code><span class="nt"><input</span> <span class="na">id=</span><span class="s">"payment"</span> <span class="na">name=</span><span class="s">"payment"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">required</span> <span class="na">pattern=</span><span class="s">"[0-9]{14,16}"</span><span class="nt">></span>
</code></pre></div>
<p></p>
<h2>
<a name="as-i-build-form-first-i-create-submit-button-so-that-i-could-keep-checking-if-the-information-is-getting-sent-correctly" href="#as-i-build-form-first-i-create-submit-button-so-that-i-could-keep-checking-if-the-information-is-getting-sent-correctly" class="anchor">
</a>
As I build form, first I create submit button so that I could keep checking if the information is getting sent correctly.
</h2>
<p></p>
<div class="highlight"><pre class="highlight html"><code><span class="nt"><form</span> <span class="na">action=</span><span class="s">"story.html"</span> <span class="na">method=</span><span class="s">"GET"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">value=</span><span class="s">"Form My Story!"</span><span class="nt">></span>
<span class="nt"></form></span>
</code></pre></div>
<p></p>
<h2>
<a name="add-radio-button-type-to-form-use-radio-button-when-i-want-boolean-value-as-my-answer-ex-yesno-one-or-the-other" href="#add-radio-button-type-to-form-use-radio-button-when-i-want-boolean-value-as-my-answer-ex-yesno-one-or-the-other" class="anchor">
</a>
Add radio button type to form. Use radio button when I want boolean value as my answer. ex: yes/no, one or the other
</h2>
<p><code>name</code> attribute in <code><input></code> groups the radio buttons' data together. Data gets submitted name: "value" pair when whichever one of yes or no is selected.<br>
</p>
<div class="highlight"><pre class="highlight html"><code>
<span class="nt"><form</span> <span class="na">action=</span><span class="s">"story.html"</span> <span class="na">method=</span><span class="s">"GET"</span><span class="nt">></span>
<span class="nt"><span></span>Yes or No:<span class="nt"></span></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">id=</span><span class="s">"yes"</span> <span class="na">name=</span><span class="s">"answer"</span> <span class="na">value=</span><span class="s">"yes"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"yes"</span><span class="nt">></span>Yes<span class="nt"></label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">id=</span><span class="s">"no"</span> <span class="na">name=</span><span class="s">"answer"</span> <span class="na">value=</span><span class="s">"no"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"no"</span><span class="nt">></span>No<span class="nt"></label></span>
<span class="nt"><br></span>
<span class="nt"></form></span>
</code></pre></div>
<p></p>
<h2>
<a name="to-create-drop-down-form-use-raw-ltselectgt-endraw-and-raw-ltoption-valuefastergtfasterltoptiongt-endraw-raw-option-valuefaster-endraw-is-the-data-that-gets-sent-to-the-server" href="#to-create-drop-down-form-use-raw-ltselectgt-endraw-and-raw-ltoption-valuefastergtfasterltoptiongt-endraw-raw-option-valuefaster-endraw-is-the-data-that-gets-sent-to-the-server" class="anchor">
</a>
To create drop down form, use <code><select></code> and <code><option value="faster">Faster</option></code>. <code>option value="faster"</code> is the data that gets sent to the server.
</h2>
<p></p>
<div class="highlight"><pre class="highlight html"><code> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"speed"</span><span class="nt">></span>Relative speed (ends in -er):<span class="nt"></label></span>
<span class="nt"><select</span> <span class="na">name=</span><span class="s">"speed"</span> <span class="na">id=</span><span class="s">"speed"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"faster"</span><span class="nt">></span>Faster<span class="nt"></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"slower"</span><span class="nt">></span>Slower<span class="nt"></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"speedier"</span><span class="nt">></span>Speedier<span class="nt"></option></span>
<span class="nt"><option</span> <span class="na">value=</span><span class="s">"lazier"</span><span class="nt">></span>Lazier<span class="nt"></option></span>
<span class="nt"></select></span>
<span class="nt"><br></span>
</code></pre></div>
<p></p>
<p class="codepen" data-height="265" data-theme-id="dark" data-default-tab="html,result" data-user="heggy231" data-slug-hash="KKdyRYG" style="height: 265px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="drop down list form">
<span>See the Pen <a href="https://codepen.io/heggy231/pen/KKdyRYG">
drop down list form</a> by Heggyhere (<a href="https://codepen.io/heggy231">@heggy231</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
<h3>
<a name="resource-on-form" href="#resource-on-form" class="anchor">
</a>
resource on <a href="https://www.codecademy.com/learn/paths/web-development/tracks/learn-html-web-dev-path/modules/learn-html-forms/cheatsheet">form</a>
</h3>
<h3>
<a name="form-example" href="#form-example" class="anchor">
</a>
<a href="https://codepen.io/heggy231/pen/bGVoyaZ">form example</a>
</h3>
<h3>
<a name="form-validation" href="#form-validation" class="anchor">
</a>
<a href="https://codepen.io/heggy231/pen/eYpeZqB">form validation</a>
</h3>
<h3>
<a name="form-login-validation" href="#form-login-validation" class="anchor">
</a>
<a href="https://codepen.io/heggy231/pen/QWjOEWb">form login validation</a>
</h3>
<p>PS: My principle when learning:</p>
<blockquote>
<p>Don't seek to know everything but seek to figure out everything.</p>
<ul>
<li><a href="https://watchandcode.com/">WatchandCode</a></li>
</ul>
</blockquote>
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)