Chapterبەش 8 · Lectureوانە 8.2

Form Handling with GET and POSTمامەڵەکردن لەگەڵ فۆرم بە GET و POST

How a form sends data to PHP: the action, method and name attributes, $_GET and $_POST, where GET and POST data really travel, when to use which, and how to send a form to the same page safely with $_SERVER["PHP_SELF"].

چۆن زانیاری بۆ PHP دەنێرێت: action و method و name، $_GET و $_POST، زانیاریی GET و POST بەڕاستی بە کوێدا دەڕۆن، کەی کامیان بەکاربهێنیت و چۆن بە سەلامەتی بە $_SERVER["PHP_SELF"] بۆ هەمان لاپەڕە بنێریت.

21 min read21 خولەک خوێندنەوە · 18 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain what action, method and name do in a form.
  • Receive form data in PHP with $_POST and $_GET.
  • Describe where GET and POST data travel, and choose the right method.
  • Correct common myths: POST is not encrypted and not unlimited.
  • Send a form to the same page safely.
  • Predict which fields arrive in $_POST and which do not.
  • ڕوون بکەیتەوە action و method و name لە چی دەکەن.
  • زانیاریی لە PHPـدا بە $_POST و $_GET وەربگریت.
  • باس بکەیت زانیاریی GET و POST بە کوێدا دەڕۆن و شێوازی گونجاو هەڵبژێریت.
  • بیرۆکە هەڵە باوەکان ڕاست بکەیتەوە: POST شفرە نەکراوە و بێسنووریش نییە.
  • بە سەلامەتی بۆ هەمان لاپەڕە بنێریت.
  • پێشبینی بکەیت کام دەگاتە $_POST و کامیان ناگات.

Forms: how a page collects informationفۆرم: لاپەڕە چۆن زانیاری کۆ دەکاتەوە

HTML forms are used to collect information from users and send it to a particular place to be processed. That place is a PHP file on the , named in the form's action attribute. In PHP, the $_GET and $_POST are used to collect the form data.

HTML بۆ کۆکردنەوەی زانیاری لە و ناردنی بۆ شوێنێکی دیاریکراو بەکار دێن، تاکو لەوێ کاری لەسەر بکرێت. ئەو شوێنە PHPـە لەسەر ، کە ناوەکەی لە نووسراوە. لە PHPـدا، $_GET و $_POST بۆ وەرگرتنی زانیارییەکانی بەکار دێن.

This is called . It is the most common job of a PHP programmer: a login page, a search box, a registration form and a comment box all work the same way. In this lecture you will send form data with GET and with POST, receive it in PHP, and learn which method to choose.

ئەمە پێی دەوترێت . باوترین کاری بەرنامەسازێکی PHPـە: لاپەڕەی ، سندووقی گەڕان، و شوێنی نووسینی تێبینی هەموویان بە هەمان شێوە کار دەکەن. لەم وانەیەدا زانیاریی بە GET و بە POST دەنێریت، لە PHPـدا وەریدەگریتەوە و فێر دەبیت کام شێواز هەڵبژێریت.

An application form at an officeفۆرمی داواکاری لە فەرمانگەیەکدا

Think of an application form at a government office. You fill in the boxes, and every box has a label printed next to it ("Name", "Phone"). The top of the paper says which desk it must go to. You can send it in two ways. With GET it is like a postcard: everything you wrote is on the outside, and anyone who handles it can read it — but you can also photocopy it and hand it in again later. With POST it is like a letter in an envelope: the address is on the outside, the content is inside.

داواکاری لە فەرمانگەیەکی حکومی بهێنە بەرچاوت. چوارگۆشەکان پڕ دەکەیتەوە و لە تەنیشت هەر چوارگۆشەیەکەوە ناوێک چاپ کراوە («ناو»، «تەلەفۆن»). سەرەوەی کاغەزەکە دەڵێت دەبێت بۆ کام مێز بڕوات. دەتوانیت بە دوو شێوە بینێریت. بە GET وەک کارتپۆستاڵ وایە: هەموو ئەوەی نووسیوتە لە دەرەوەیە و هەر کەسێک دەستی پێی بگات دەتوانێت بیخوێنێتەوە — بەڵام دەتوانیت کۆپیشی بکەیت و دواتر دووبارە بیدەیتەوە. بە POST وەک نامەیەکی ناو زەرف وایە: ناونیشانەکە لە دەرەوەیە، ناوەڕۆکەکە لە ناوەوە.

Where the analogy stops workingسنووری ئەم نموونەیە

An envelope is closed with glue; POST data is not. Anyone on the way can open it unless the site uses . "Inside the envelope" means only "not in the address bar".

زەرف بە چەسپ دادەخرێت؛ بەڵام زانیاریی POST داناخرێت. هەر کەسێک لە ڕێگادا بێت دەتوانێت بیکاتەوە، مەگەر بەکاربهێنێت. «لەناو زەرفدا» تەنها واتای «لە نییە» دەدات.

How form data reaches PHPزانیاریی فۆرم چۆن دەگاتە PHP

Three attributes of the form decide everything:

سێ بڕیار لەسەر هەموو شتێک دەدەن:

  • action — where the data goes: the PHP file that will receive it, e.g. action="welcome.php". If it is empty or missing, the form is sent back to the same page.
  • method — how the data travels: get (in the URL) or post (in the request body). It also decides which array PHP fills: $_GET or $_POST.
  • name on each field — the key. The text the user typed becomes the value. A field without a name is never sent.
  • action — زانیارییەکان بۆ کوێ دەڕۆن: ئەو PHPـەی وەریاندەگرێت، بۆ نموونە action="welcome.php". ئەگەر بەتاڵ بێت یان نەبێت، بۆ هەمان لاپەڕە دەنێردرێتەوە.
  • method — زانیارییەکان چۆن دەڕۆن: get (لەناو URL) یان post (لەناو ). هەروەها بڕیار دەدات PHP کام پڕ بکاتەوە: $_GET یان $_POST.
  • name لەسەر هەر — . ئەو دەقەی نووسیویەتی دەبێتە . کە nameـی نەبێت هەرگیز نانێردرێت.
Four steps of a form submission. 1: In the browser the user fills in Name and E-mail and clicks Submit; the browser sends an HTTP request that carries the form data. 2: On the web server, Apache finds welcome.php and PHP fills the array, for example $_POST["name"] = "Aram". 3: Your code reads the value and builds an HTML page. 4: The response, which contains only HTML and no PHP code, goes back to the browser, which shows "Welcome Aram". Below: with method="get" the data is added to the URL and PHP puts it in $_GET; with method="post" the data travels inside the request body and PHP puts it in $_POST.چوار هەنگاوی ناردنی فۆرمێک. ١: لە تۆڕگەڕدا بەکارهێنەر ناو و ئیمەیڵ دەنووسێت و کرتە لە Submit دەکات؛ تۆڕگەڕەکە داواکارییەکی HTTP دەنێرێت کە زانیارییەکانی فۆرمەکە هەڵدەگرێت. ٢: لەسەر ڕاژەکاری وێب، Apache فایلی welcome.php دەدۆزێتەوە و PHP ڕیزەکەکە پڕ دەکاتەوە، بۆ نموونە $_POST["name"] = "Aram". ٣: کۆدەکەت نرخەکە دەخوێنێتەوە و لاپەڕەیەکی HTML دروست دەکات. ٤: وەڵامەکە، کە تەنها HTMLـی تێدایە و هیچ کۆدێکی PHPـی تێدا نییە، دەگەڕێتەوە بۆ تۆڕگەڕ و تۆڕگەڕەکە «Welcome Aram» پیشان دەدات. لە خوارەوە: بە method="get" زانیارییەکان دەخرێنە سەر URL و PHP دەیانخاتە ناو $_GET؛ بە method="post" زانیارییەکان لەناو جەستەی داواکاریدا دەڕۆن و PHP دەیانخاتە ناو $_POST.
From the form to the result page: the request and the responseلە بۆ لاپەڕەی ئەنجام: و وەڵامCêIT
How the form and the PHP file are linked. The attribute method="post" decides that PHP fills $_POST. The field name="name", where the user typed Aram, becomes $_POST["name"] with the value "Aram". The field name="email" becomes $_POST["email"]. The submit button has no name, so it is not sent. Rules: get goes to $_GET and post goes to $_POST; the name becomes the key; keys are case-sensitive; a field without a name is never sent.پەیوەندیی نێوان فۆرمەکە و فایلی PHPـەکە. خەسڵەتی method="post" بڕیار دەدات کە PHP $_POST پڕ بکاتەوە. خانەی نێوخستەی name="name"، کە بەکارهێنەر Aramـی تێدا نووسیوە، دەبێتە $_POST["name"] بە نرخی "Aram". خانەی نێوخستەی name="email" دەبێتە $_POST["email"]. دوگمەی ناردنەکە nameـی نییە، بۆیە نانێردرێت. یاساکان: get بۆ $_GET و post بۆ $_POST؛ name دەبێتە کلیل؛ کلیلەکان هەستیار بە قەبارەی پیتن؛ خانەیەکی نێوخستە کە nameـی نەبێت هەرگیز نانێردرێت.
method chooses the array, name becomes the keymethod هەڵدەبژێرێت، name دەبێتە CêIT
Quick checkپشکنینی خێراA form has <input type="text" name="city"> and <input type="text"> (with no name). The user fills in both. What does PHP receive? <input type="text" name="city"> و <input type="text">ـی هەیە (دووەمیان nameـی نییە). هەردووکیان پڕ دەکاتەوە. PHP چی وەردەگرێت؟

6 | The Basics of PHP Form Handling Tutorial | 2023 | Learn PHP Full Course for BeginnersDani Krossing · Englishئینگلیزی · 244K viewsبینین · 4.8K likesلایک · Partبەشی 0:00–8:35 (ofلە 23:50)

Building a form with action, method and name, then reading the sent values in a PHP handler file, and why PHP must check data again.

دروستکردنی بە action، method و name، پاشان خوێندنەوەی لە و بۆچی دەبێت دووبارە بپشکنێتەوە.

Watch 0:00–8:35: form attributes and reading $_POST in PHP

لە ⁦0:00⁩ تا ⁦8:35⁩ سەیر بکە: و خوێندنەوەی $_POST لە .

Example: sending a form with POSTنموونە: ناردنی فۆرم بە POST

The form below has two text fields and a submit button. Its action is welcome.php and its method is post.

ئەم خوارەوە دوو و هەیە. actionـەکەی welcome.phpـە و methodـەکەی postـە.

index.htmlHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<form action="welcome.php" method="post">
  Name: <input type="text" name="name"><br>
  E-mail: <input type="text" name="email"><br>
  <input type="submit">
</form>

The course shows welcome.php like this. Each name from the form is the key in $_POST:

کۆرسەکە welcome.php بەم شێوەیە پیشان دەدات. هەر nameێک لە لە $_POSTدا:

welcome.php as in the course⁦welcome.php⁩ وەک لە کۆرسەکەداwelcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>

Example: sending a form with POSTنموونە: ناردنی فۆرم بە POST

Opening welcome.php directly, and a safe versionکردنەوەی ڕاستەوخۆی welcome.php و وەشانێکی سەلامەت

It works — but only in the normal case. Open welcome.php directly in the browser, without sending the form, and PHP 8 complains, because $_POST is empty:

کار دەکات — بەڵام تەنها لە دۆخی ئاساییدا. ئەگەر welcome.php ڕاستەوخۆ لە بکەیتەوە، بەبێ ناردنی ، ⁦PHP 8⁩ دەدات، چونکە $_POST بەتاڵە:

welcome.php opened directly (no POST data)⁦welcome.php⁩ ڕاستەوخۆ کرایەوە (بێ زانیاریی POST)
Welcome
Warning: Undefined array key "name" in C:\xampp\htdocs\forms\welcome.php on line 3

Your email address is:
Warning: Undefined array key "email" in C:\xampp\htdocs\forms\welcome.php on line 4
A safe welcome.php⁦welcome.php⁩ـی سەلامەتwelcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<!DOCTYPE html>
<html>
<body>

Welcome <?php echo htmlspecialchars($_POST["name"] ?? ""); ?><br>
Your email address is: <?php echo htmlspecialchars($_POST["email"] ?? ""); ?>

</body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-3

A complete HTML page. The course snippet showed only the <body> part.

لاپەڕەیەکی تەواوی HTML. پارچەکەی کۆرسەکە تەنها بەشی <body>ـی پیشان دابوو.

Lineدێڕی 5

$_POST["name"] ?? "" gives an empty string when nothing was sent, without a warning.

$_POST["name"] ?? "" کاتێک هیچ نەنێردرابێت بەتاڵ دەدات، بەبێ .

Lineدێڕی 5-6

htmlspecialchars() turns <, >, & and quotes into harmless text.

htmlspecialchars() هێماکانی < و > و & و دەکاتە دەقی بێزیان.

Opened directly: no warnings any moreڕاستەوخۆ کرایەوە: ئیتر هۆشداری نییە
Welcome
Your email address is:
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>

Welcome <br>
Your email address is: 
</body>
</html>
Quick checkپشکنینی خێراNothing was sent with POST. What does $_POST["name"] ?? "" give?هیچ شتێک بە POST نەنێردراوە. $_POST["name"] ?? "" چی دەدات؟

Example: sending a form with GETنموونە: ناردنی فۆرم بە GET

Now the same form with method="get" and a different receiving file, welcome_get.php. The only change in PHP is the array: $_GET instead of $_POST.

ئێستا هەمان بە method="get" و وەرگری جیاواز، welcome_get.php. تاکە گۆڕانکاری لە PHPـدا : $_GET لە جیاتی $_POST.

index.htmlHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<form action="welcome_get.php" method="get">
  Name: <input type="text" name="name"><br>
  E-mail: <input type="text" name="email"><br>
  <input type="submit">
</form>
welcome_get.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<!DOCTYPE html>
<html>
<body>

Welcome <?php echo htmlspecialchars($_GET["name"] ?? ""); ?><br>
Your email address is: <?php echo htmlspecialchars($_GET["email"] ?? ""); ?>

</body>
</html>

After clicking Submit, look at the address bar. The browser added the data to the URL as a : welcome_get.php?name=Aram&email=aram%40example.com. Each pair is name=value; the pairs are joined with &; special characters are encoded (@ became %40). PHP decodes them again before it fills $_GET.

دوای کرتەکردن لە Submit، سەیری بکە. زانیارییەکانی وەک خستە سەر URL: welcome_get.php?name=Aram&email=aram%40example.com. هەر جووتێک name=valueـە؛ جووتەکان بە & بەیەکەوە دەبەسترێن؛ هێماکانی وەک @ کۆد دەکرێن (@ بووە %40). PHP پێش پڕکردنەوەی $_GET دووبارە دەیانکاتەوە بە شێوەی ئاسایی.

Quick checkپشکنینی خێراYou send the GET form with the name Aram. Where can you see name=Aram afterwards? GET بە ناوی Aram دەنێریت. دواتر لە کوێ name=Aram دەبینیت؟

GET or POST?GET یان POST؟

Both methods send form data to the server, and in PHP both arrive in an associative array. The difference is where the data travels inside the HTTP , and what follows from that.

هەردوو شێوازەکە زانیاریی بۆ دەنێرن و لە PHPـدا هەردووکیان لە دەگەن. جیاوازییەکە لەوەدایە کە زانیارییەکان لەناو HTTPـدا بە کوێدا دەڕۆن و ئەوەی لەوەوە دەردەچێت.

Two HTTP requests that carry the same data. GET: the address bar shows localhost/forms/welcome_get.php?name=Aram&email=aram%40example.com; the request line is GET /forms/welcome_get.php?name=Aram&email=aram%40example.com HTTP/1.1, followed by the headers and no body. So the data is visible in the address bar, the browser history, bookmarks and server logs, and PHP reads it with $_GET["name"]. POST: the address bar shows only localhost/forms/welcome.php; the request line POST /forms/welcome.php HTTP/1.1 carries no data; after the headers (Content-Type: application/x-www-form-urlencoded, Content-Length: 34) and an empty line, the body contains name=Aram&email=aram%40example.com, and PHP reads it with $_POST["name"]. POST data is not in the URL, but it is not secret either; use HTTPS.دوو داواکاریی HTTP کە هەمان زانیاری هەڵدەگرن. GET: شریتی URL localhost/forms/welcome_get.php?name=Aram&email=aram%40example.com پیشان دەدات؛ دێڕی داواکارییەکە GET /forms/welcome_get.php?name=Aram&email=aram%40example.com HTTP/1.1ـە، پاشان سەردێڕەکانی HTTP دێن و هیچ جەستەیەک نییە. بۆیە زانیارییەکان لە شریتی URL، مێژووی تۆڕگەڕ، نیشانەکانی لاپەڕە و لۆگەکانی ڕاژەکاردا دیارن و PHP بە $_GET["name"] دەیانخوێنێتەوە. POST: شریتی URL تەنها localhost/forms/welcome.php پیشان دەدات؛ دێڕی POST /forms/welcome.php HTTP/1.1 هیچ زانیارییەکی تێدا نییە؛ دوای سەردێڕەکانی HTTP (Content-Type: application/x-www-form-urlencoded و Content-Length: 34) و دێڕێکی بەتاڵ، جەستەکە name=Aram&email=aram%40example.comـی تێدایە و PHP بە $_POST["name"] دەیخوێنێتەوە. زانیاریی POST لەناو URL نییە، بەڵام نهێنیش نییە؛ HTTPS بەکاربهێنە.
Where the data travels: in the URL (GET) or in the request body (POST)زانیارییەکان بە کوێدا دەڕۆن: لەناو ⁦URL (GET)⁩ یان لەناو ⁦(POST)⁩CêIT
GET compared with POSTبەراوردی GET و POST
QuestionپرسیارGETPOST
Where the data travelsزانیارییەکان بە کوێدا دەڕۆنIn the URL, as a query string: page.php?q=phpلەناو URL، وەک : page.php?q=phpIn the body of the HTTP request, after the headersلەناو HTTP، دوای
PHP array PHP$_GET$_POST
Visible in the address barلە دیارەYesبەڵێNoنەخێر
Saved in browser history, bookmarks and server logsلە مێژووی ، و لۆگەکانی دەمێنێتەوەYes — the whole URL with the dataبەڵێ — هەموو URLـەکە لەگەڵ زانیارییەکانNo — only the address, not the dataنەخێر — تەنها ناونیشانەکە، نەک زانیارییەکان
Can be shared or bookmarkedدەتوانرێت بڵاو بکرێتەوە یان بکرێتە YesبەڵێNoنەخێر
Cached by the browser لە هەڵیدەگرێتCan beدەکرێتNormally notبە ئاسایی نا
Amount of dataبڕی زانیاریLimited by browsers and servers (roughly 2,000 characters is a safe maximum) و سنووری بۆ دادەنێن (نزیکەی ٢٠٠٠ سنوورێکی سەلامەتە)Much larger; limited by post_max_size in php.ini (default 8M)زۆر گەورەترە؛ post_max_size لە php.ini سنووری بۆ دادەنێت (بنەڕەتی 8M)
Files (binary data such as images or Word documents) (دراوەی دووانی، وەک وێنە یان بەڵگەنامەی Word)NoنەخێرYes, with enctype="multipart/form-data"بەڵێ، بە enctype="multipart/form-data"
Encrypted?شفرە کراوە؟Only with HTTPS (and even then it stays in history and logs)تەنها بە HTTPS (و ئەو کاتیش لە مێژوو و لۆگەکاندا دەمێنێتەوە)Only with HTTPSتەنها بە HTTPS
Use it forبۆ چی بەکاری بهێنەSearching, filtering, page numbers, links: reading data, with nothing privateگەڕان، پاڵاوتن، ژمارەی لاپەڕە، : خوێندنەوەی زانیاری، بێ هیچ شتێکی تایبەتPasswords, private data, long text, files, and anything that changes data (register, save, delete)، زانیاریی تایبەت، دەقی درێژ، و هەر شتێک کە زانیاری بگۆڕێت (، پاشەکەوت، سڕینەوە)
Quick checkپشکنینی خێراWhich form should use GET?کام دەبێت GET بەکاربهێنێت؟

GET or POST?GET یان POST؟

Myths about GET and POSTبیرۆکە هەڵەکان دەربارەی GET و POST

Quick checkپشکنینی خێراIs POST data encrypted?ئایا زانیاریی POST شفرە کراوە؟

Sending a form to the same pageناردنی فۆرم بۆ هەمان لاپەڕە

Often the form and the PHP that processes it are in one file. When the form is sent, the same page loads again, PHP sees the data and shows the result (or error messages) next to the form. To do this, the action must point to the page itself. There are three ways:

زۆرجار و ئەو PHPـەی کاری لەسەر دەکات لە یەک . کاتێک دەنێردرێت، هەمان لاپەڕە دووبارە دادەبەزێت، PHP زانیارییەکان دەبینێت و ئەنجامەکە (یان ) لە تەنیشت پیشان دەدات. بۆ ئەمە دەبێت action خودی لاپەڕەکە نیشان بدات. سێ ڕێگا هەیە:

  • action="" or no action at all — the browser sends the form to the current address. This is the simplest and safest way.
  • action="#" — also the current page (the course's drop-down example uses it).
  • action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" — PHP prints the path of the current script. This is the course's way.
  • action="" یان هەر action نەبێت — بۆ ناونیشانی ئێستا دەنێرێت. ئەمە سادەترین و سەلامەترین ڕێگایە.
  • action="#" — ئەمیش هەمان لاپەڕەیە (نموونەی کۆرسەکە بەکاری دەهێنێت).
  • action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" — PHP ئێستاکە چاپ دەکات. ئەمە ڕێگاکەی کۆرسەکەیە.

Lab 5 calls this Example 3, "validating form inputs": to validate form inputs we use if statements, so the information the user enters is sent to the same page — with # in the action attribute, or with the superglobal $_SERVER["PHP_SELF"]. The checks themselves are in the validation lecture.

تاقیگەی ٥ ئەمە بە «نموونەی ٣: نێوخستەکانی » ناو دەبات: بۆ نێوخستەکانی بەکار دەهێنین، بۆیە ئەو زانیارییانەی دەینووسێت بۆ هەمان لاپەڕە دەنێردرێن — بە # لە actionدا، یان بە $_SERVER["PHP_SELF"]. خودی پشکنینەکان لە وانەی پشتڕاستکردنەوەدان.

$_SERVER["PHP_SELF"] is an entry of the superglobal $_SERVER that returns the file name of the currently running script — more exactly, its path from the web root, for example /form_handling/index.php.

$_SERVER["PHP_SELF"] بڕگەیەکی $_SERVERـە کە ناوی ئەو ئێستا کار دەکات دەگەڕێنێتەوە — بە وردی، لە ڕەگی ، بۆ نموونە /form_handling/index.php.

The same file opened normally as /form_handling/index.phpهەمان فایل بە شێوەی ئاسایی وەک /⁦form_handling/index.php⁩ کرایەوە
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!-- UNSAFE: PHP_SELF printed as it is -->
<form method="post" action="/form_handling/index.php">

<!-- SAFE: PHP_SELF escaped with htmlspecialchars() -->
<form method="post" action="/form_handling/index.php">
Quick checkپشکنینی خێراWhat is wrong with action="<?php $_SERVER['PHP_SELF'] ?>"?چ هەڵەیەک لە action="<?php $_SERVER['PHP_SELF'] ?>"دا هەیە؟

Sending a form to the same pageناردنی فۆرم بۆ هەمان لاپەڕە

The course form: text, textarea and radio buttonsفۆرمەکەی کۆرسەکە: دەق، ناوچەی دەق و دوگمەی ڕادیۆ

Lecture 5 and Lab 5 build one bigger form that sends itself to the same page. It has three text fields (name, e-mail, website), a for a comment (5 rows, 40 columns), two for gender that share name="gender" (so only one can be chosen), and a submit button with name="submit". Here is the corrected code:

وانەی ٥ و تاقیگەی ٥ گەورەتر دروست دەکەن کە خۆی بۆ هەمان لاپەڕە دەنێرێت. سێ هەیە (ناو، ، )، بۆ تێبینی (rows="5" و cols="40")، دوو بۆ ڕەگەز کە هەردووکیان name="gender"یان هەیە (بۆیە تەنها یەکێکیان هەڵدەبژێردرێت) و بە name="submit". ئەمە کۆدە ڕاستکراوەکەیە:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="text" name="email">
  <br><br>
  Website: <input type="text" name="website">
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <br><br>
  <input type="submit" name="submit" value="Submit">
</form>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The form posts back to the same script; PHP_SELF is escaped.

بۆ هەمان دەنێردرێتەوە؛ PHP_SELF سەلامەت کراوە.

Lineدێڕی 8

A textarea has a closing tag, and its text goes between the tags (there is no value attribute).

داخستنی هەیە و دەقەکەی دەکەوێتە نێوان ( valueـی نییە).

Lineدێڕی 11-12

Both radio buttons use the same name; each has its own value. Only the chosen value is sent.

هەردوو هەمان name بەکار دەهێنن؛ هەریەکەیان valueـی خۆی هەیە. تەنها هەڵبژێردراوەکە دەنێردرێت.

Lineدێڕی 14

name="submit" lets PHP ask isset($_POST["submit"]): "was the form sent?"

name="submit" ڕێگە بە PHP دەدات بپرسێت isset($_POST["submit"]): «ئایا نێردرا؟»

The form as the course shows it. The asterisks, "* required field." and "Your Input:" come from the fuller version you will build in the next two lectures. وەک کۆرسەکە پیشانی دەدات. ئەستێرەکان، «* ⁦required field⁩.» و «⁦Your Input⁩:» لە وەشانە تەواوەکەوە هاتوون کە لە دوو وانەی داهاتوودا دروستی دەکەیت.Course material

Sending a form to the same pageناردنی فۆرم بۆ هەمان لاپەڕە

What PHP receives from each kind of fieldPHP لە هەر جۆرە توخمێکی فۆرم چی وەردەگرێت

The quickest way to understand a form is to print $_POST with print_r(). Here is what arrives when Dilan fills in the form above and clicks Submit:

خێراترین ڕێگا بۆ تێگەیشتن لە چاپکردنی $_POSTـە بە print_r(). ئەمە ئەوەیە کە دەگات کاتێک دیلان سەرەوە پڕ دەکاتەوە و کرتە لە Submit دەکات:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";

Now Dilan leaves e-mail, website and comment empty and chooses no gender:

ئێستا دیلان ، و تێبینی بەتاڵ بەجێدەهێڵێت و هیچ ڕەگەزێک هەڵنابژێرێت:

Empty fields, no radio button chosenخانەکانی نێوخستە بەتاڵن، هیچ دوگمەیەکی ڕادیۆ هەڵنەبژێردراوە
Array
(
    [name] => Dilan
    [email] =>
    [website] =>
    [comment] =>
    [submit] => Submit
)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>Array
(
    [name] => Dilan
    [email] => 
    [website] => 
    [comment] => 
    [submit] => Submit
)
</pre>
  • Text fields and textareas are always sent, even when empty: website arrives as an empty string "".
  • A radio group with nothing chosen is not sent at all: there is no gender key. Reading $_POST["gender"] would give a warning; check it with isset() or empty() (next lecture).
  • An unticked checkbox is not sent either; a ticked one sends its value.
  • The submit button is sent only if it has a name. Here it sends submit => Submit (its value), which is how PHP knows the form was sent.
  • All values arrive as strings, even numbers: an age field sends "20", not 20.
  • و هەمیشە دەنێردرێن، تەنانەت ئەگەر بەتاڵیش بن: website وەک بەتاڵ "" دەگات.
  • کۆمەڵە کە هیچیان هەڵنەبژێردرابێت هەر نانێردرێت: gender نییە. خوێندنەوەی $_POST["gender"] دەدات؛ بە isset() یان empty() بیپشکنە (وانەی داهاتوو).
  • نیشانە نەکراویش نانێردرێت؛ ئەوەی نیشانە کرابێت valueـەکەی دەنێرێت.
  • تەنها کاتێک دەنێردرێت کە nameـی هەبێت. لێرەدا submit => Submit دەنێرێت (valueـەکەی) و PHP بەمە دەزانێت نێردراوە.
  • هەموو وەک دەگەن، تەنانەت ژمارەکانیش: تەمەن "20" دەنێرێت، نەک 20.
Quick checkپشکنینی خێراWhich of these arrives in $_POST even when the user leaves it alone?کامیان دەگاتە $_POST تەنانەت ئەگەر دەستی لێ نەدات؟

Lab 5: your first form-handling projectتاقیگەی ٥: یەکەم پرۆژەی مامەڵەکردن لەگەڵ فۆرم

Lab stepsهەنگاوەکانی تاقیگە
  1. Create the projectپرۆژەکە دروست بکە

    Open a new PHP project in your editor and name it form_handling. The lab uses NetBeans; with XAMPP you can also simply create the folder C:\xampp\htdocs\form_handling and open it in any editor.

    پرۆژەیەکی نوێی PHP لە دەستکاریکەرەکەتدا بکەرەوە و ناوی بنێ form_handling. تاقیگەکە NetBeans بەکار دەهێنێت؛ بە XAMPP دەتوانیت بە سادەیی C:\xampp\htdocs\form_handling دروست بکەیت و لە هەر دەستکاریکەرێکدا بیکەیتەوە.

  2. Write the formفۆرمەکە بنووسە

    Write the form in index.php (Example 1 below).

    لە index.phpدا بنووسە (نموونەی ١ی خوارەوە).

  3. Write the receiving pageلاپەڕەی وەرگر بنووسە

    Create a second file, welcome.php, that reads the value and prints it.

    دووەم، welcome.php، دروست بکە کە بخوێنێتەوە و چاپی بکات.

  4. Test itتاقی بکەرەوە

    Open http://localhost/form_handling/ in the browser, type an e-mail address and press Enter.

    http://localhost/form_handling/ لە بکەرەوە، بنووسە و Enter دابگرە.

Lab 5: your first form-handling projectتاقیگەی ٥: یەکەم پرۆژەی مامەڵەکردن لەگەڵ فۆرم

Example 1: reading a value sent with POSTنموونەی ١: خوێندنەوەی نرخێک کە بە POST نێردراوە

index.phpHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<form action="welcome.php" method="post">
  Email: <input type="text" name="uemail" placeholder="email address">
</form>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The data goes to welcome.php with POST.

زانیارییەکان بە POST دەچن بۆ welcome.php.

Lineدێڕی 2

name="uemail" becomes the key $_POST["uemail"]. The next attribute shows a grey hint inside the empty box; the hint is not sent as a value.

name="uemail" دەبێتە $_POST["uemail"]. دواتر ڕێنماییەکی خۆڵەمێشی لەناو سندووقە بەتاڵەکەدا پیشان دەدات؛ ئەو ڕێنماییە وەک نانێردرێت.

welcome.php as in the lab⁦welcome.php⁩ وەک لە تاقیگەکەداwelcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php

echo "your email address is ".$_POST["uemail"];
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The dot . joins the text and the value (concatenation).

خاڵ . دەق و پێکەوە دەلکێنێت.

welcome.php opened directly⁦welcome.php⁩ ڕاستەوخۆ کرایەوە
Warning: Undefined array key "uemail" in C:\xampp\htdocs\form_handling\welcome.php on line 3
your email address is 

The same improvement as before: give a default and escape the value. Note that a file that contains only PHP may leave out the closing ?>; this is even recommended, because nothing can accidentally be printed after it.

هەمان چاککردنەوەی پێشوو: بدە و سەلامەت بکە. سەرنج بدە کە تەنها PHPـی تێدایە دەتوانێت ?>ی کۆتایی نەنووسێت؛ ئەمە تەنانەت پێشنیاریش دەکرێت، چونکە هیچ شتێک بە هەڵە لە دوایەوە چاپ نابێت.

welcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$email = $_POST["uemail"] ?? "";

echo "Your email address is " . htmlspecialchars($email);

Lab 5: your first form-handling projectتاقیگەی ٥: یەکەم پرۆژەی مامەڵەکردن لەگەڵ فۆرم

Example 2: the same with GETنموونەی ٢: هەمان شت بە GET

The lab repeats the form with method="get", but the sheet ends after <?php and never shows the GET version of welcome.php. Here is the missing part: only the array changes.

تاقیگەکە بە method="get" دووبارە دەکاتەوە، بەڵام پەڕەکە دوای <?php کۆتایی دێت و هەرگیز وەشانی GETـی welcome.php پیشان نادات. ئەمە بەشە ونبووەکەیە: تەنها دەگۆڕێت.

index.phpHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<form action="welcome.php" method="get">
  Email: <input type="text" name="uemail" placeholder="email address">
</form>
welcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$email = $_GET["uemail"] ?? "";

echo "Your email address is " . htmlspecialchars($email);

Common mistakesهەڵە باوەکان

  • Forgetting name. <input type="text"> without name is never sent; PHP sees nothing.
  • Reading the wrong array. The form says method="post" but the PHP reads $_GET["name"] (or the other way round). A form without method uses GET.
  • Different spelling of the key. name="Color" must be read as $_POST["Color"]; keys are case-sensitive.
  • Reading values without checking. Opening the receiving page directly gives Undefined array key warnings. Use isset() or ??.
  • Printing user input as it is. Always htmlspecialchars() it, or your page is open to XSS.
  • Printing $_SERVER["PHP_SELF"] without escaping, or writing it without echo.
  • Using GET for passwords or private data: it stays in the URL, history and logs.
  • Opening the PHP file from the disk (file:///C:/…/welcome.php) instead of through the server (http://localhost/…). Without the server, PHP never runs.
  • Curly quotes copied from a document: name=”email” or $_POST[“email”]. Code needs straight quotes ".
  • لەبیرکردنی name. <input type="text"> بەبێ name هەرگیز نانێردرێت؛ PHP هیچ نابینێت.
  • خوێندنەوەی نادروست. method="post"ـی تێدایە بەڵام PHPـەکە $_GET["name"] دەخوێنێتەوە (یان بە پێچەوانەوە). کە methodـی نەبێت GET بەکار دەهێنێت.
  • ڕێنووسی جیاوازی . name="Color" دەبێت وەک $_POST["Color"] بخوێنرێتەوە؛ هەستیارن بە قەبارەی پیت.
  • خوێندنەوەی بێ پشکنین. کردنەوەی ڕاستەوخۆی لاپەڕەی وەرگر Undefined array key دەدات. isset() یان ?? بەکاربهێنە.
  • چاپکردنی وەک خۆی. هەمیشە htmlspecialchars()ـی بۆ بەکاربهێنە، ئەگینا لاپەڕەکەت بۆ XSS کراوەیە.
  • چاپکردنی $_SERVER["PHP_SELF"] بێ سەلامەتکردن، یان نووسینی بێ echo.
  • بەکارهێنانی GET بۆ یان زانیاریی تایبەت: لە URL، مێژوو و لۆگەکاندا دەمێنێتەوە.
  • کردنەوەی PHPـەکە لە دیسکەوە (file:///C:/…/welcome.php) لە جیاتی لە ڕێگەی (http://localhost/…). بەبێ ، PHP هەرگیز کار ناکات.
  • چەماوە کە لە بەڵگەنامەیەکەوە کۆپی کراون: name=”email” یان $_POST[“email”]. کۆد پێویستی بە ڕاست " هەیە.

Practiceڕاهێنان

Quick checkپشکنینی خێراA form has method="post" and a field <input name="city">. Which line reads the value in PHP? method="post" و <input name="city">ـی هەیە. کام دێڕ لە PHPـدا دەخوێنێتەوە؟

Quick checkپشکنینی خێراWhere does POST put the form data?POST زانیارییەکانی لە کوێ دادەنێت؟

Quick checkپشکنینی خێراA form has no method attribute. In which array does PHP find its data? methodـی نییە. PHP زانیارییەکانی لە کام دەدۆزێتەوە؟

ExerciseڕاهێنانTwo text boxes and a submit buttonدوو خانەی دەق و دوگمەیەکی ناردن

Make a page with a form that has two text boxes (first name and last name) and a submit button. The form sends itself to the same page with POST. When it was sent, print Hello, <first> <last>! under the form. Make the output safe.

لاپەڕەیەک دروست بکە کە تێدا بێت بە دوو (ناوی یەکەم و ناوی کۆتایی) و . بە POST خۆی بۆ هەمان لاپەڕە بنێرێت. کاتێک نێردرا، لە ژێر Hello, <first> <last>! چاپ بکە. سەلامەت بکە.

Hintئاماژە

Give the submit button a name and test it with isset().

nameێک بدە بە و بە isset() بیپشکنە.

Show answerوەڵامەکە پیشان بدە
index.phpPHP
<form method="post" action="">
  First name: <input type="text" name="first"><br>
  Last name: <input type="text" name="last"><br>
  <input type="submit" name="send" value="Send">
</form>

<?php
if (isset($_POST["send"])) {
    $first = $_POST["first"] ?? "";
    $last  = $_POST["last"] ?? "";
    echo "<p>Hello, " . htmlspecialchars($first) . " " . htmlspecialchars($last) . "!</p>";
}
?>
ExerciseڕاهێنانGET or POST?GET یان POST؟

Choose GET or POST for each form and give one reason: (a) a weather page where the user picks a city; (b) a registration form with a password; (c) a form to write a long comment; (d) page 3 of a list of news; (e) a "Delete my account" button.

بۆ هەر GET یان POST هەڵبژێرە و هۆکارێک بنووسە: (أ) لاپەڕەیەکی کەشوهەوا کە شارێک هەڵدەبژێرێت؛ (ب) بە ؛ (ج) بۆ نووسینی تێبینییەکی درێژ؛ (د) لاپەڕەی ٣ی لیستێکی هەواڵ؛ (هـ) دوگمەی «هەژمارەکەم بسڕەوە».

Show answerوەڵامەکە پیشان بدە
  • (a) GET — it only reads data, and weather.php?city=Erbil can be bookmarked.
  • (b) POST — a password must never be in the URL (and use HTTPS).
  • (c) POST — long text, and it saves something.
  • (d) GET — news.php?page=3 is a normal, shareable link.
  • (e) POST — it changes data; a GET link could be triggered just by visiting it.
  • (أ) GET — تەنها زانیاری دەخوێنێتەوە و weather.php?city=Erbil دەتوانرێت نیشانە بکرێت.
  • (ب) POST — نابێت هەرگیز لەناو URLـدا بێت (و HTTPS بەکاربهێنە).
  • (ج) POST — دەقی درێژە و شتێک پاشەکەوت دەکات.
  • (د) GET — news.php?page=3 ئاسایی و بڵاوکراوەیە.
  • (هـ) POST — زانیاری دەگۆڕێت؛ GET دەتوانێت تەنها بە سەردانکردنی کارا ببێت.
ExerciseڕاهێنانFind the bugsهەڵەکان بدۆزەوە

This form should send the user's city to city.php, which prints it. Nothing arrives. Find three mistakes.

ئەم دەبێت شاری بنێرێت بۆ city.php، کە چاپی دەکات. هیچ ناگات. سێ هەڵە بدۆزەوە.

index.htmlHTML
<form action="city.php" method="post">
  City: <input type="text">
  <input type="submit">
</form>
city.phpPHP
<?php
echo "Your city is " . $_GET["City"];
Show answerوەڵامەکە پیشان بدە
  • The input has no name, so nothing is sent: add name="city".
  • The form uses POST but the PHP reads $_GET: use $_POST.
  • The key "City" does not match name="city" (case): use "city". Also add ?? "" and htmlspecialchars().
  • nameـی نییە، بۆیە هیچ نانێردرێت: name="city" زیاد بکە.
  • POST بەکار دەهێنێت بەڵام PHPـەکە $_GET دەخوێنێتەوە: $_POST بەکاربهێنە.
  • "City" لەگەڵ name="city" ناگونجێت (قەبارەی پیت): "city" بەکاربهێنە. هەروەها ?? "" و htmlspecialchars() زیاد بکە.
city.phpPHP
<?php
$city = $_POST["city"] ?? "";
echo "Your city is " . htmlspecialchars($city);
Review questionsپرسیارەکانی پێداچوونەوە
What are HTML forms used for?فۆرمەکانی HTML بۆ چی بەکار دێن؟

To collect information from users and send it to a particular place — the PHP file in action — to be processed.

بۆ کۆکردنەوەی زانیاری لە و ناردنی بۆ شوێنێکی دیاریکراو — ئەو PHPـەی لە actionدایە — تاکو کاری لەسەر بکرێت.

Which two superglobals collect form data, and how do you choose?کام دوو سووپەرگلۆباڵ زانیاریی فۆرم کۆ دەکەنەوە و چۆن هەڵیدەبژێریت؟

$_GET and $_POST. The form's method decides: get fills $_GET, post fills $_POST.

$_GET و $_POST. methodـی بڕیار دەدات: get ـ$_GET پڕ دەکاتەوە، post ـ$_POST.

Give three differences between GET and POST.سێ جیاوازیی نێوان GET و POST بنووسە.

GET puts the data in the URL (visible, bookmarkable, saved in history and logs, short); POST puts it in the request body (not in the URL, can be large, can carry files). GET is for reading/searching; POST is for private data and for changing data.

GET زانیارییەکان دەخاتە ناو URL (دیارە، نیشانە دەکرێت، لە مێژوو و لۆگەکاندا دەمێنێتەوە، کورتە)؛ POST دەیانخاتە ناو (لەناو URL نییە، دەتوانێت گەورە بێت، دەتوانێت ببات). GET بۆ خوێندنەوە/گەڕانە؛ POST بۆ زانیاریی تایبەت و گۆڕینی زانیارییە.

What does $_SERVER["PHP_SELF"] return, and why must it be escaped?$_SERVER["PHP_SELF"] چی دەگەڕێنێتەوە و بۆچی دەبێت سەلامەت بکرێت؟

The path of the running script (e.g. /form_handling/index.php). It contains what the visitor typed in the address, so printing it raw allows XSS; wrap it in htmlspecialchars() or simply use action="".

ئەو کار دەکات (بۆ نموونە /form_handling/index.php). ئەوەی تێدایە کە سەردانکەر لە ناونیشانەکەدا نووسیویەتی، بۆیە چاپکردنی وەک خۆی ڕێگە بە XSS دەدات؛ بیخە ناو htmlspecialchars() یان بە سادەیی action="" بەکاربهێنە.

Which fields are not sent when the user leaves them alone?کام توخمی فۆرم نانێردرێت ئەگەر بەکارهێنەر دەستی لێ نەدات؟

A radio group with nothing chosen, an unticked checkbox, any field without name, and a submit button without name. Empty text fields are sent, as "".

کۆمەڵە کە هیچیان هەڵنەبژێردرابێت، نیشانە نەکراو، هەر کە nameـی نەبێت و بێ name. بەتاڵ دەنێردرێت، وەک "".

Is POST "unlimited"?ئایا POST «بێسنوورە»؟

No. PHP limits it with post_max_size (default 8 MB) and uploads with upload_max_filesize (default 2 MB), set in php.ini.

نەخێر. PHP بە post_max_size (بنەڕەتی ٨ ) سنووری بۆ دادەنێت و بە upload_max_filesize (بنەڕەتی ٢ )، کە لە php.iniدا دادەنرێن.

Summaryپوختە

  • A form's action says where the data goes, method says how, and each field's name becomes a key.
  • method="get" → data in the URL → $_GET. method="post" → data in the request body → $_POST.
  • Use GET for searching and links; use POST for passwords, private or long data, files and changes.
  • POST is not encrypted; only HTTPS protects data on the way. Neither method is "unlimited".
  • To send a form to the same page use action="", or echo htmlspecialchars($_SERVER["PHP_SELF"]) — never raw PHP_SELF.
  • Empty text fields arrive as ""; unchosen radio buttons and unticked checkboxes do not arrive at all.
  • Read values with isset() or ?? and print them with htmlspecialchars().
  • actionـی دەڵێت زانیارییەکان بۆ کوێ دەچن، method دەڵێت چۆن و nameـی هەر دەبێتە .
  • method="get" ← زانیاری لەناو URL ← $_GET. method="post" ← زانیاری لەناو ← $_POST.
  • GET بۆ گەڕان و بەکاربهێنە؛ POST بۆ ، زانیاریی تایبەت یان درێژ، و گۆڕانکاری.
  • POST شفرە نەکراوە؛ تەنها HTTPS زانیارییەکان لە ڕێگادا دەپارێزێت. هیچ کام لە شێوازەکان «بێسنوور» نییە.
  • بۆ ناردنی بۆ هەمان لاپەڕە action="" بەکاربهێنە، یان echo htmlspecialchars($_SERVER["PHP_SELF"]) — هەرگیز PHP_SELFـی خاو.
  • بەتاڵ وەک "" دەگات؛ هەڵنەبژێردراو و نیشانە نەکراو هەر ناگەن.
  • بە isset() یان ?? بخوێنەوە و بە htmlspecialchars() چاپیان بکە.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

PHP $_GET and $_POST explainedBro Code · Englishئینگلیزی · 94K viewsبینین · 2.5K likesلایک

How a form sends data with GET (in the URL) and with POST (in the request body), and how PHP reads it from $_GET and $_POST.

چۆن بە (لەناو ) و بە (لەناو ) دەنێرێت و چۆن لە $_GET و $_POST دەیخوێنێتەوە.

POST is not encrypted and has size limits; see 'Myths about GET and POST'.

POST تێدا نییە و سنووری قەبارەی هەیە؛ سەیری بەشی Myths about GET and POST بکە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Form Validationپشتڕاستکردنەوەی فۆرم

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.