What form elements do we know / have we used? →
What are some other form elements that you've seen for collecting data? →
Some newer ones…
There's also a hidden input:
Of course, all of these elements have to be nested within a parent <form> element.
And…. if you want to be able to submit a form (of course you do), you'll probably want a submit button:
<form method="POST" action="">
<!-- other form element placed within form tags-->
<input type="submit">
</form>
Each of these form elements have different markup:
type
attributeBut there are some element attributes that are common to all of them →:
name
req.body
or req.query
objectsvalue
req.body
or req.query
objectsText, email and url are, as their names imply, input fields for text, emails and urls. They're all <input>
elements. They vary by type:
<label>Text</label><input type="text" name="textExample">
<label>Email</label><input type="email" name="emailExample">
<label>URL</label><input type="url" name="urlExample">
Range and number represent a numeric value, and again, they're both <input> elements. They have a type of range
and number
. However, instead of regular text entry…
Here's some markup:
<label>Number</label><input type="number" min="1" max="10" step="1" name="numberExample">
<label>Range</label><input min="1" max="10" step="1" type="range" name="rangeExample">
Did you see any extra attributes there? →
The following attributes are optional:
min
- the lower bound of the form elementmax
- the upper bound of the form elementstep
- the interval between values (the increment/decrement from one value to the next)Radio buttons allow you to choose only one value from a set of values. They're <input> elements with a type of radio
.
<label>Radio 1</label><input type="radio" name="radioExample" value="1">
<label>Radio 2</label><input type="radio" name="radioExample" value="2">
<label>Radio 3</label><input type="radio" name="radioExample" value="3">
What's peculiar about the way they're named and the values that they're given? What value do you think is sent over to the server? →
checked
attribute to have a default radio button selectedCheck boxes allow you to choose multiple values from a set of values. They're <input> elements with a type of checkbox
.
<label>Checkbox 1</label>
<input type="checkbox" name="checkboxExample" value="1">
<label>Checkbox 2</label>
<input type="checkbox" name="checkboxExample" value="2">
<label>Checkbox 3</label>
<input type="checkbox" name="checkboxExample" value="3">
checked
attribute to the tagreq.body.checkboxExample
→ [1, 2]
Date shows up as a date picker in your browser. It is an <input> element with a type of date
<label>Date</label><input type="date" name="dateExample">
yyyy-mm-dd
formatdatetime
, week
, month
, etc.Dropdowns represent a form element that lets you pick a single value from a set of values (Similar to radio buttons). However, the markup for dropdowns uses a single <select> element (with the name
attribute), and <option> elements (with the value
) nested within it:
<select name="selectExample">
<option value="Option 1">Option 1</option>
<option value="Option 2" selected>Option 2</option>
<option value="Option 3">Option 3</option>
</select>
option
element to default to that optionmultiple
, on the select
element…
A larger version of text input!
<textarea name="textareaExample"></textarea>
(not much to see here)
This input will send along the data that's in the value
attribute when the form is submitted. It's not visible in the user interface.
<input type="hidden" name="hiddenExample" value="someValue>
Particularly useful if you want a pre-set piece of data to be submitted along with the rest of your form (perhaps the id of an associated resource).
In a router…
router.get('/forms', function(req, res) {
res.render('forms', { title: 'Express' });
});
router.post('/forms', function(req, res) {
console.log('=====\n', req.body, '\n=====');
res.redirect('/forms');
});
In forms.hbs: