ਇੱਕ <form> ਵਰਤੋਂਕਾਰ ਦੀ ਇਨਪੁਟ ਨੂੰ ਇਕੱਠਾ ਕਰਦਾ ਹੈ ਅਤੇ ਇਸਨੂੰ ਸਰਵਰ ਨੂੰ submit ਕਰਦਾ ਹੈ। ਮੁੱਖ attributes ਹਨ action (ਕਿੱਥੇ ਭੇਜਣਾ) ਅਤੇ method (ਕਿਵੇਂ ਭੇਜਣਾ)।
html
Email
Password
Sign up
ਇੱਕ <form> ਵਰਤੋਂਕਾਰ ਦੀ ਇਨਪੁਟ ਨੂੰ ਇਕੱਠਾ ਕਰਦਾ ਹੈ ਅਤੇ ਇਸਨੂੰ ਸਰਵਰ ਨੂੰ submit ਕਰਦਾ ਹੈ। ਮੁੱਖ attributes ਹਨ action (ਕਿੱਥੇ ਭੇਜਣਾ) ਅਤੇ method (ਕਿਵੇਂ ਭੇਜਣਾ)।
Email
Password
Sign up
ਜਦੋਂ ਵਰਤੋਂਕਾਰ submit ਬਟਨ ਤੇ ਕਲਿੱਕ ਕਰਦਾ ਹੈ (ਜਾਂ Enter ਦਬਾਉਂਦਾ ਹੈ), ਬ੍ਰਾ਼ਉਜ਼ਰ ਸਾਰੇ controls ਨੂੰ ਇਕੱਠਾ ਕਰਦਾ ਹੈ ਜਿਨ੍ਹਾਂ ਕੋਲ name ਹੈ, ਉਨ੍ਹਾਂ ਨੂੰ encode ਕਰਦਾ ਹੈ, ਅਤੇ action ਨੂੰ request ਭੇਜਦਾ ਹੈ।
<form method="get" action="/search"> <!-- data in the URL: /search?q=html -->
<form method="post" action="/login"> <!-- data in the request body -->
name attribute ਜरুरी ਹੈ<input value="hi" /> <!-- ❌ no name → NOT submitted -->
<input name="message" value="hi" /> <!-- ✅ submitted as message=hi -->
ਸਿਰਫ਼ named controls ਭੇਜੇ ਜਾਂਦੇ ਹਨ। ਸਰਵਰ ਉਨ੍ਹਾਂ ਨੂੰ ਉਨ੍ਹਾਂ ਦੇ name ਦੁਆਰਾ ਪੜ੍ਹਦਾ ਹੈ।
form.addEventListener("submit", (e) => {
e.preventDefault(); // stop the full-page reload to handle it with fetch/AJAX
const data = new FormData(form); // easy way to read all fields
});
Forms ਉਹ ਮੁੱਖ ਤਰੀਕਾ ਹੈ ਜਿਸ ਦੁਆਰਾ ਵਰਤੋਂਕਾਰ ਸਰਵਰ ਨੂੰ data ਭੇਜਦੇ ਹਨ।
action/method, name ਦੀ ਭੂਮਿਕਾ, GET-versus-POST semantics, ਅਤੇ JS ਹਨਡਲਿੰਗ ਲਈ preventDefault ਨੂੰ ਸਮਝਣਾ ਹਰ web app ਵਿੱਚ input ਦੇ ਨਾਲ ਮੂਲ ਗੱਲ ਹੈ।