<form> هو حاوية لعناصر الإدخال وأداة إرسال البيانات للخادم.
أهم الصفات: • action: رابط الصفحة/API التي تستقبل البيانات • method: طريقة الإرسال: - GET: البيانات في URL (للبحث والفلاتر) - POST: البيانات مخفية في جسم الطلب (للتسجيل وكلمات المرور) • enctype: ترميز البيانات: - application/x-www-form-urlencoded: الافتراضي - multipart/form-data: ضروري لرفع الملفات - text/plain: نادر الاستخدام • novalidate: تعطيل التحقق الافتراضي • autocomplete: on/off للإكمال التلقائي • target: _self/_blank لأين تعرض النتيجة
| 1 | <!-- نموذج تسجيل دخول --> |
| 2 | <form action="/api/login" method="POST" autocomplete="on"> |
| 3 | <label for="email">البريد الإلكتروني</label> |
| 4 | <input type="email" id="email" name="email" required> |
| 5 | |
| 6 | <label for="password">كلمة المرور</label> |
| 7 | <input type="password" id="password" name="password" required> |
| 8 | |
| 9 | <button type="submit">تسجيل الدخول</button> |
| 10 | </form> |
| 11 | |
| 12 | <!-- نموذج بحث (GET) --> |
| 13 | <form action="/search" method="GET"> |
| 14 | <input type="search" name="q" placeholder="ابحث..."> |
| 15 | <button type="submit">بحث</button> |
| 16 | </form> |
| 17 | |
| 18 | <!-- نموذج رفع ملف --> |
| 19 | <form action="/upload" method="POST" enctype="multipart/form-data"> |
| 20 | <input type="file" name="document"> |
| 21 | <button type="submit">رفع</button> |
| 22 | </form> |
مثال 1: نموذج تسجيل كامل
| 1 | <form |
| 2 | action="/api/register" |
| 3 | method="POST" |
| 4 | autocomplete="on" |
| 5 | novalidate |
| 6 | > |
| 7 | <fieldset> |
| 8 | <legend>معلومات الحساب</legend> |
| 9 | |
| 10 | <div class="form-group"> |
| 11 | <label for="fullname">الاسم الكامل</label> |
| 12 | <input type="text" id="fullname" name="fullname" |
| 13 | placeholder="أحمد محمد علي" required> |
| 14 | </div> |
| 15 | |
| 16 | <div class="form-group"> |
| 17 | <label for="reg-email">البريد الإلكتروني</label> |
| 18 | <input type="email" id="reg-email" name="email" |
| 19 | placeholder="ahmed@example.com" required> |
| 20 | </div> |
| 21 | |
| 22 | <div class="form-group"> |
| 23 | <label for="reg-password">كلمة المرور</label> |
| 24 | <input type="password" id="reg-password" name="password" |
| 25 | minlength="8" required> |
| 26 | </div> |
| 27 | </fieldset> |
| 28 | |
| 29 | <button type="submit">إنشاء الحساب</button> |
| 30 | </form> |