Lesson 23 / الدرس 23
Introduction to forms / مقدمة إلى النماذج
A form is how a page collects something from the visitor and sends it somewhere. You need four things: <form>, action, method, and an input with a label.
النموذج هو الطريقة التي تجمع بها الصفحة شيئًا من الزائر وترسله إلى مكان ما. وتحتاج فيه إلى أربعة أشياء: <form> و action و method وحقل إدخال له تسمية.
Everything you have written so far only shows things. A form is the first thing on a page that does something: the visitor types, presses a button, and the browser packs up what they typed and sends it to a server.
<form action="/search" method="get">
<label for="q">What are you looking for?</label>
<input type="text" id="q" name="q">
<button type="submit">Search</button>
</form>
The <form> element and its two attributes
<form> والسمتان المهمتان فيه
The <form> element wraps every field that should be sent together. It carries two attributes that decide what happens when the visitor presses the button: action is the address the data is sent to, and method is how it is sent.
<form> يحيط بكل الحقول التي تُرسل معًا. وهو يحمل سمتين تحددان ما يحدث عند ضغط الزر: action هي العنوان الذي تُرسل إليه البيانات، و method هي طريقة إرسالها.GET or POST
With method="get" the browser puts the values in the address bar, after a ?. That suits a search: the address can be bookmarked and sent to a friend, and asking for it again changes nothing. With method="post" the values travel inside the request instead, out of the address bar. Use it for anything private, anything long, and anything that changes something — a password, a comment, an order.
method="get" يضع المتصفح القيم في شريط العنوان بعد علامة ?، وهذا مناسب للبحث: يمكن حفظ العنوان في المفضلة وإرساله إلى صديق، وطلبه مرة أخرى لا يغيّر شيئًا. أما مع method="post" فتذهب القيم داخل الطلب نفسه بعيدًا عن شريط العنوان، وتُستعمل مع كل ما هو خاص، أو طويل، أو يغيّر شيئًا: كلمة مرور، أو تعليق، أو طلب شراء.<form action="/search" method="get">
<input type="text" name="q">
<button type="submit">Search</button>
</form>
<form action="/comments" method="post">
<input type="text" name="comment">
<button type="submit">Send</button>
</form>
/search?q=dates; the second shows nothing at all in the address.
/search?q=dates، والثاني لا يُظهر شيئًا في العنوان.Every field needs a label
A <label> is the text that names a field. Link the two by giving the input an id and giving the label a for with the same value. Then the browser knows they belong together: a screen reader announces the label when the visitor reaches the field, and clicking the label puts the cursor inside the box.
<label> هو النص الذي يسمّي الحقل. واربط بينهما بأن تعطي الحقل سمة id وتعطي التسمية سمة for بالقيمة نفسها، فيعرف المتصفح أنهما مرتبطان: يقرأ قارئ الشاشة التسمية عندما يصل الزائر إلى الحقل، والنقر على التسمية يضع المؤشر داخل الصندوق.placeholder ليس تسمية؛ فالنص الرمادي داخل الصندوق يختفي بمجرد أن يبدأ الزائر بالكتابة، وقد لا يقرأه قارئ الشاشة أصلًا. اكتب <label> حقيقية، وأبقِ placeholder لمثال على الإجابة.name لا يُرسل أبدًا. يملؤه الزائر ويضغط الزر فتختفي القيمة في صمت، ولا يبدو في الصفحة أي خطأ. فسمة id تربط التسمية بالحقل داخل المتصفح، أما name فهو الاسم الذي يراه الخادم. والنموذج الحقيقي يحتاج إليهما معًا.| Attribute | Goes on | What it does |
|---|---|---|
| action | <form> | The address the data is sent to |
| method | <form> | get or post |
| name | <input> | The name the server receives the value under |
| id | <input> | Connects the field to its label |
| for | <label> | Holds the id of the field it names |
| type | <input> | text gives a one-line text box |
Every attribute used in this lesson.
Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does the action attribute do?
action؟action is the address and method is the way. They sit side by side on the <form>, which is why they are easy to confuse.
action هي العنوان، و method هي الطريقة. وهما متجاورتان على وسم <form>، ولهذا يسهل الخلط بينهما.2. Which method puts the values in the address bar?
get adds the values to the address after a ?, which is why it suits a search and not a password.
get تُلحق القيم بالعنوان بعد علامة ?، ولهذا تناسب البحث ولا تناسب كلمة المرور.3. You filled in a text box and its value never arrived. What is missing?
The browser only sends fields that have a name. This is the classic silent bug in a first form.
name. وهذا هو الخطأ الصامت الشهير في أول نموذج تكتبه.Score / النتيجة: 0 / 3
Your task / مهمتك
Build a "contact us" form: a heading, then a form that sends to /contact with method="post" and holds two text fields — a name and a subject — each with its own label, and a button that submits it.
ابنِ نموذج "اتصل بنا": عنوان، ثم نموذج يُرسل إلى /contact بطريقة method="post"، وفيه حقلا نص — الاسم والموضوع — لكل منهما تسميته، وزر يرسل النموذج.
-
The
<form>has bothactionandmethodوسم<form>فيهactionوmethodمعًا -
Two text inputs, each with a
nameحقلا نص، لكل منهما سمةname -
Each label's
formatches theidof its field سمةforفي كل تسمية تطابقidحقلها - A button that submits the form زر يرسل النموذج