|
Learning objective 11.5.3.4 create HTML forms for data entry
HTML FormsCreating HTML forms for data entry
Forms allow a website to collect information from users. In this lesson, you will explore common form controls, choose appropriate input types, and create your own HTML form. Success criteriaBy the end of the lesson, I can:
Why? Recall Form structure Input types Explorer Attributes Radio vs Checkbox Check yourself Debug Practical task Vocabulary
1. Why do websites need forms?Imagine this situation.
Your school is opening a new Coding Club. Students should be able to register online. The website needs to collect:
How can a web page collect all this information? The answer is an HTML form. Forms create an interface through which users can enter, select and submit data. 1 User enters data
Name, email, password, date, number... 2 User makes choices
Radio buttons, checkboxes and lists. 3 User submits the form
The browser prepares the entered values for processing. 4 Application processes data
Later, server-side code can store the information in a database. 2. Recall: HTML basicsBefore learning forms, check that you remember the basic HTML elements. Which HTML element would you use for each purpose?
Check your answers1. <h1> 2. <p> 3. <a> 4. <img> 5. <tr> Remember:
HTML describes the structure and meaning of the page. CSS controls its visual presentation. 3. The <form> elementThe <form> element groups controls that are used to collect data from a user. <form> <label for="studentName"> Student name: </label> <input type="text" id="studentName" name="studentName" required> <input type="submit" value="Submit"> </form> Use HTML and CSS editorBrowser result
4. Common input typesThe behaviour of an <input> depends on its type attribute.
5. Interactive Form ExplorerSelect an element on the left. Experiment with the real form control and compare the browser result with its HTML code. type="text"
Text input<label for="studentName"> Student name: </label> <input type="text" id="studentName" name="studentName"> Use type="text" for short text such as a name, city, class or username.
type="email"
Email input<label for="email"> School email: </label> <input type="email" id="email" name="email" placeholder="student@school.kz"> An email input tells the browser that an email address is expected.
type="password"
Password input<input type="password" id="password" name="password"> Type something into the field. Notice that the characters are hidden on the screen.
type="number"
Number input<input type="number" id="age" name="age" min="10" max="20"> Try changing the value. The min and max attributes define limits.
type="date"
Date input<input type="date" id="birthDate" name="birthDate"> Click the field. Most modern browsers display a built-in date picker.
type="radio"
Radio buttonsChoose your grade: <input type="radio" name="grade" value="10"> Grade 10 <input type="radio" name="grade" value="11"> Grade 11 Try selecting two grades at the same time. You cannot because the radio buttons have the same name.
type="checkbox"
CheckboxesSelect programming interests: <input type="checkbox" name="interest" value="html"> HTML Try selecting all three options. Checkboxes allow multiple independent selections.
<select>
Drop-down list<select name="day"> <option value="tue"> Tuesday </option> <option value="wed"> Wednesday </option> </select> The <select> element contains <option> elements.
<textarea>
Textarea<textarea id="reason" name="reason" rows="4"> </textarea> Use <textarea> when the user needs to enter longer, multi-line text.
type="submit"
Submit button<input type="submit" value="Send application"> The Submit button starts the process of submitting the form data.
6. Important form attributesConsider this input element: <input type="email" id="schoolEmail" name="email" placeholder="name@school.kz" required> typeDefines what kind of input control the browser should create. type="email"idUniquely identifies the element on the page. It can also connect the input to a label. id="schoolEmail"nameIdentifies the data when the form is submitted. name="email"placeholderDisplays a short hint inside an empty field. placeholder="name@school.kz"requiredSpecifies that the user must complete the field before the form can be submitted. requiredvalueSpecifies the value associated with a control. It is particularly important for radio buttons, checkboxes and options. value="html"Why should we use <label>?<label for="email"> School email: </label> <input type="email" id="email" name="email"> The for value in the label should match the id of its form control.
Try clicking the words School email in a correctly labelled form. The browser focuses the associated input field. 7. Radio buttons vs CheckboxesThese controls may look similar, but they solve different problems. Radio buttonsUse when exactly one option should be selected. Try selecting all three. CheckboxesUse when several options may be selected. Try selecting all three. Think:
A form asks a student which programming languages they know. A student may know Python, JavaScript and C++. Should you use radio buttons or checkboxes? Show answerCheckboxes. Several answers may be selected at the same time.
8. Check your understandingChoose an answer. Feedback appears immediately. 1. Which input type is most appropriate for a school email?
✓ Correct. type="email" is designed for email-address input.
✗ Try again. Think about the type of data being entered.
2. Which attribute identifies data when a form is submitted?
✓ Correct. The name attribute identifies the submitted data.
✗ Try again. Which attribute becomes the name of the submitted value?
3. A student must choose exactly one year group. Which control is most appropriate?
✓ Correct. Radio buttons are appropriate for one choice from a group.
✗ Try again. The user must choose only one option.
4. Which element is best for a long written comment?
✓ Correct. <textarea> is designed for multi-line text.
✗ Try again. Which control allows several lines of text?
9. Debug the formA student wrote the following code: <form> <label for="email"> Email: </label> <input type="text" id="studentEmail"> <p>Choose your grade:</p> <input type="radio" name="grade10"> Grade 10 <input type="radio" name="grade11"> Grade 11 </form> Find at least three problems.
Think about:
Show explanationProblem 1: The email field should preferably use type="email". Problem 2: The label uses for="email", but the input has id="studentEmail". These values should match. Problem 3: The email input has no name. Problem 4: The two radio buttons use different names. Therefore, they do not form one exclusive-choice group. Corrected versionShow corrected code<form> <label for="studentEmail"> Email: </label> <input type="email" id="studentEmail" name="email"> <p>Choose your grade:</p> <label> <input type="radio" name="grade" value="10"> Grade 10 </label> <label> <input type="radio" name="grade" value="11"> Grade 11 </label> </form> 10. Common mistakes❌ Wrong controlUsing checkboxes when only one answer should be selected. Better: use radio buttons. ❌ Different radio namesRadio buttons that belong to the same group have different name values. Better: give them the same name. ❌ Missing nameThe input looks correct but has no name attribute. The form-processing system then cannot identify the submitted value properly. ❌ Placeholder instead of labelA placeholder is a hint. It should not normally replace a proper <label>. ❌ Everything is textUsing type="text" for email, numbers, dates and passwords. Choose the most appropriate input type. ❌ Wrong label connectionThe label's for value does not match the input's id. 11. Practical ChallengeSchool Coding Club Registration FormYour school is launching a Coding Club. Create an HTML registration form for students. Requirements
Minimum success criteria
Extension challengeImprove your form using some of these attributes: min max maxlength placeholder required Then use CSS to improve the visual design of your form. Show one possible solution<form> <label for="studentName"> Student name: </label> <input type="text" id="studentName" name="studentName" required> <label for="email"> School email: </label> <input type="email" id="email" name="email" required> <label for="age"> Age: </label> <input type="number" id="age" name="age" min="14" max="18"> <p>Year group:</p> <label> <input type="radio" name="grade" value="10"> Grade 10 </label> <label> <input type="radio" name="grade" value="11"> Grade 11 </label> <label> <input type="radio" name="grade" value="12"> Grade 12 </label> <p>Programming interests:</p> <label> <input type="checkbox" name="interest" value="html"> HTML </label> <label> <input type="checkbox" name="interest" value="python"> Python </label> <label> <input type="checkbox" name="interest" value="javascript"> JavaScript </label> <label for="day"> Preferred meeting day: </label> <select id="day" name="day"> <option value="tue"> Tuesday </option> <option value="wed"> Wednesday </option> <option value="thu"> Thursday </option> </select> <label for="reason"> Why do you want to join? </label> <textarea id="reason" name="reason" rows="5"> </textarea> <input type="submit" value="Send application"> </form> Self-assessmentBefore finishing the lesson, check your form. Vocabulary · English–Russian
Questions:Answer these questions without looking at the examples.
Check answers1. It groups controls used to collect and submit user data. 2. Radio buttons normally allow one choice from a group; checkboxes allow independent or multiple choices. 3. The name identifies a control's value when the form data is submitted. 4. type="email" 5. The label's for value should match the input's id. 6. <label for="studentName"> Student name: </label> <input type="text" id="studentName" name="studentName" required> Exercises:Ex. 1 Create a webform
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Просмотров: 920 | | | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Всего комментариев: 0 | |
