<video width="320" height="240" autoplay controls>
<source src="small.mp4" type="video/mp4">
<source src="small.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
ถ้าวิดีโออยู่บน YouTube ไม่ต้องใช้ <video> แต่ให้ใช้ <iframe> ร่วมกับ YouTube Video Id เพื่ออ้างอิงวิดีโอในโค้ด HTML ในตัวอย่างนี้ Video Id คือ 62bIsvRcPv0 ซึ่งอยู่ท้าย URL https://www.youtube.com/embed/
HTML form คือกลุ่มของชุดคำสั่งที่สร้างขึ้นเพื่อรับ input จากผู้ใช้ การสร้าง form เริ่มจาก tag <form>...</form> แล้วจึงใส่ form element ต่างๆ เช่น <input>, <button> ไว้ระหว่าง tag เปิดและปิดของ form
ตัวอย่าง form สมบูรณ์จากสไลด์ (Application Form) ประกอบด้วยกรอบ <fieldset> ที่มีหัวข้อ "Student Information" ช่องกรอก Student ID และ Name (type text) ช่องเลือกวันเกิด (type date) และปุ่ม Submit ทุก <label> ผูกกับช่องกรอกของตัวเองผ่าน for ที่ตรงกับ id
All form elements must be within <form> tags — form element ทุกตัวต้องอยู่ภายใน <form> เท่านั้น
<input> เป็นชุดคำสั่งหลักสำหรับรับค่าใน form และระบุประเภทด้วย attribute type ประเภทที่เรียนในบทนี้คือ text, password, radio, checkbox, email, tel และ date (รวมถึงตัวแปรอื่นของวันเวลา เช่น month, week, datetime-local)
type="text" และ type="password"
type="text" สร้างช่องกรอกข้อความบรรทัดเดียว (one-line text input field) ใช้คู่กับ <label> ที่มี attribute for เพื่อกำหนดป้ายชื่อให้ form element โดยค่าของ for ต้องตรงกับ id ของช่องที่ต้องการผูก
type="checkbox" สร้างกล่องเลือกที่ใช้เมื่อเลือกได้หลายตัวเลือกพร้อมกัน (multiple-choice) หลักการตั้ง name ให้เหมือนกันเพื่อจัดกลุ่ม และใช้ checked กำหนดกล่องที่ถูกเลือกไว้ตั้งแต่แรก (กี่กล่องก็ได้) เหมือนกับ radio
What should I have for lunch? <br>
<input type="checkbox" name="food" value="SW">Sandwich<br>
<input type="checkbox" name="food" value="FC">Fried Chicken<br>
<input type="checkbox" name="food" value="HB">Hamburger<br>
radio
เลือกได้ 1 ตัวเลือก ต่อกลุ่ม (กลุ่ม = name เดียวกัน)
What is your mobile phone?
<input type="tel" name="mobile" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<!-- Pattern: XXX-XXX-XXXX -->
<button type="submit">Submit</button>
type="email" สร้างช่องสำหรับอีเมล ซึ่งจะถูกตรวจสอบว่าเป็นรูปแบบของอีเมลเมื่อ submit form โดยไม่ต้องเขียน pattern เอง
What is your email?
<input type="email" name="userEmail">
<button type="submit">Submit</button>
type="date" แสดง date picker ให้ผู้ใช้เลือกวันที่ และจำกัดช่วงวันที่ได้ด้วย attribute min และ max ในตัวอย่างนี้ max="2010-01-01" หมายความว่าเลือกวันที่หลังจาก 1 มกราคม 2010 ไม่ได้
When is your birthday?
<input type="date" name="bd_d" max="2010-01-01">
นอกจาก date ยังมีตัวแปรอื่นที่เกี่ยวกับวันและเวลาอีก ได้แก่ type="week", type="month", type="datetime-local" และ type="time"
placeholder เป็นแค่ข้อความ hint ในช่อง ไม่ได้บังคับให้กรอก · required คือตัวที่บังคับให้กรอกก่อน submit · title อธิบาย pattern ให้ผู้ใช้เข้าใจ
ตัวอย่างจากสไลด์ใช้ครบหลายตัว: ช่อง Mobile มี pattern และ title อธิบายรูปแบบ, ช่อง E-mail มี placeholder, required และ title, ช่อง Country code มี pattern="[A-Z]{3}" พร้อม title
<form>
First name:<br><input type="text" name="fname"><br>
Last name:<br><input type="text" name="lname"><br>
Mobile:<br>
<input type="tel" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Mobile: XXX-XXX-XXXX"><br>
E-mail:<br>
<input type="email" name="userid" placeholder="Your e-mail" required
title="Required e-mail"><br>
Country code:<br>
<input type="text" pattern="[A-Z]{3}" title="A country code with three alphabet"><br>
<p><button type="submit">Submit</button></p>
</form>
Form Submission
เมื่อผู้ใช้คลิกปุ่ม submit ข้อมูลทั้งหมดใน form จะถูกส่งออกไปยังหน้าปลายทางหรือ web server เพื่อประมวลผล การส่งถูกกำหนดด้วย attribute 2 ตัวของ <form>
action
กำหนดว่าจะส่ง form-data ไปที่ไหน เมื่อ submit
method
กำหนด HTTP method (GET/POST) ที่ใช้ส่ง form-data
Form ทำงานอย่างไร (How the Form works)
ผู้ใช้กรอกข้อมูลแล้วกดปุ่ม Submit
browser ทำ Form Validation
ถ้า form ไม่ผ่าน (invalid) จะย้อนกลับไปที่ form ให้ผู้ใช้แก้ไข ข้อมูลยังไม่ถูกส่ง
ถ้าผ่าน validation จะไปยัง server หรือปลายทางที่กำหนดใน action ด้วย HTTP method GET หรือ POST
วาดใหม่จากสไลด์ 42 "How the Form works" รวมกับสไลด์ 41 เรื่อง GET/POST — รายละเอียดฝั่ง server จะได้เรียนตอนทำ form handling
GET vs POST
GET
ส่งข้อมูลทั้งหมดผ่าน URL — ข้อมูลที่ได้รับจะแสดงอยู่ใน url path เป็นคู่ของ name และ value ซึ่ง name ตรงกับชื่อ (attribute name) ของช่องใน form ที่ส่งมา
คำว่า Semantic แปลว่า "มีความหมาย" (related to meaning in language or logic) semantic elements คือ tag ที่ใช้กำหนดส่วนต่างๆ ของหน้าเว็บ หรือใช้วางโครงสร้างของหน้าเว็บ (layout) ซึ่งจะนำไปใช้ร่วมกับ CSS ต่อไป
<article>
<h2>Famous Cities</h2>
<article>
<h2>London</h2>
<p>London is the capital city of England.</p>
</article>
<article>
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</article>
<article>
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</article>
</article>
<aside> — ข้อความในตัวอย่างสไลด์บอกหลักการไว้เองว่า "This content should be related to the surrounding content." คือเนื้อหาข้างเคียงควรเกี่ยวข้องกับเนื้อหารอบๆ แต่แยกออกมาเป็นส่วนเสริม เช่น sidebar
<aside><!-- Sidebar -->
<h2>
This content should be related<br>
to the surrounding content.
</h2>
</aside>