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"] بۆ هەمان لاپەڕە بنێریت.
What you will learnچی فێر دەبیت
- Explain what
action,methodandnamedo in a form. - Receive form data in PHP with
$_POSTand$_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
$_POSTand 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ـدا وەریدەگریتەوە و فێر دەبیت کام شێواز هەڵبژێریت.
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) orpost(in the request body). It also decides which array PHP fills:$_GETor$_POST.nameon each field — the key. The text the user typed becomes the value. A field without anameis never sent.
action— زانیارییەکان بۆ کوێ دەڕۆن: ئەو PHPـەی وەریاندەگرێت، بۆ نموونەaction="welcome.php". ئەگەر بەتاڵ بێت یان نەبێت، بۆ هەمان لاپەڕە دەنێردرێتەوە.method— زانیارییەکان چۆن دەڕۆن:get(لەناو URL) یانpost(لەناو ). هەروەها بڕیار دەدات PHP کام پڕ بکاتەوە:$_GETیان$_POST.nameلەسەر هەر — . ئەو دەقەی نووسیویەتی دەبێتە . کەnameـی نەبێت هەرگیز نانێردرێت.
method chooses the array, name becomes the keymethod هەڵدەبژێرێت، name دەبێتە CêITBuilding 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ـە.
<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>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<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دا:
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>Welcome Aram Your email address is: aram@example.com
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<body>
Welcome Aram<br>
Your email address is: aram@example.com
</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 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
<!DOCTYPE html>
<html>
<body>
Welcome <?php echo htmlspecialchars($_POST["name"] ?? ""); ?><br>
Your email address is: <?php echo htmlspecialchars($_POST["email"] ?? ""); ?>
</body>
</html>Welcome Aram Your email address is: aram@example.com
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Welcome Aram<br>
Your email address is: aram@example.com
</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()هێماکانی<و>و&و دەکاتە دەقی بێزیان.
Welcome Your email address is:
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Welcome <br>
Your email address is:
</body>
</html>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.
<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><!DOCTYPE html>
<html>
<body>
Welcome <?php echo htmlspecialchars($_GET["name"] ?? ""); ?><br>
Your email address is: <?php echo htmlspecialchars($_GET["email"] ?? ""); ?>
</body>
</html>Welcome Aram Your email address is: aram@example.com
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Welcome Aram<br>
Your email address is: aram@example.com
</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 دووبارە دەیانکاتەوە بە شێوەی ئاسایی.
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ـدا بە کوێدا دەڕۆن و ئەوەی لەوەوە دەردەچێت.
| Questionپرسیار | GET | POST |
|---|---|---|
| Where the data travelsزانیارییەکان بە کوێدا دەڕۆن | In the URL, as a query string: page.php?q=phpلەناو URL، وەک : page.php?q=php | In 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)، زانیاریی تایبەت، دەقی درێژ، و هەر شتێک کە زانیاری بگۆڕێت (، پاشەکەوت، سڕینەوە) |
GET or POST?GET یان POST؟
Myths about GET and POSTبیرۆکە هەڵەکان دەربارەی GET و POST
GET or POST?GET یان POST؟
A good use of GET: a search boxبەکارهێنانێکی باشی GET: سندووقی گەڕان
A search page is the classic GET example. The result page has its own address, so the user can bookmark it or send the link to a friend.
لاپەڕەی گەڕان نموونەی باوی GETـە. لاپەڕەی ئەنجامەکە ناونیشانی تایبەتی خۆی هەیە، بۆیە دەتوانێت نیشانەی بکات یان بۆ هاوڕێیەک بنێرێت.
<form action="search.php" method="get">
Search: <input type="text" name="q">
<input type="submit" value="Search">
</form>
<?php
if (isset($_GET["q"])) {
$q = htmlspecialchars($_GET["q"]);
echo "<p>You searched for: <b>$q</b></p>";
echo "<p>Share this page: search.php?q=" . urlencode($_GET["q"]) . "</p>";
}
?>Search: [ ] [Search] You searched for: php forms Share this page: search.php?q=php+forms
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<form action="search.php" method="get">
Search: <input type="text" name="q">
<input type="submit" value="Search">
</form>
<p>You searched for: <b>php forms</b></p><p>Share this page: search.php?q=php+forms</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The form sends itself to
search.phpwith GET.بە GET خۆی بۆ
search.phpدەنێرێت.- Lineدێڕی 7
Run the PHP only when a search word was sent.
PHPـەکە تەنها کاتێک کار دەکات کە نێردرابێت.
- Lineدێڕی 10
urlencode()prepares text for a URL: the space becomes+.urlencode()دەق بۆ URL ئامادە دەکات: دەبێتە+.
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 noactionat 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.
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">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". ئەمە کۆدە ڕاستکراوەکەیە:
<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>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<form method="post" action="/form_handling/index.php">
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_SELFis escaped.بۆ هەمان دەنێردرێتەوە؛
PHP_SELFسەلامەت کراوە.- Lineدێڕی 8
A textarea has a closing tag, and its text goes between the tags (there is no
valueattribute).داخستنی هەیە و دەقەکەی دەکەوێتە نێوان (
valueـی نییە).- Lineدێڕی 11-12
Both radio buttons use the same
name; each has its ownvalue. Only the chosen value is sent.هەردوو هەمان
nameبەکار دەهێنن؛ هەریەکەیانvalueـی خۆی هەیە. تەنها هەڵبژێردراوەکە دەنێردرێت.- Lineدێڕی 14
name="submit"lets PHP askisset($_POST["submit"]): "was the form sent?"name="submit"ڕێگە بە PHP دەدات بپرسێتisset($_POST["submit"]): «ئایا نێردرا؟»
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 دەکات:
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";Array
(
[name] => Dilan
[email] => dilan@example.com
[website] =>
[comment] => Hello!
[gender] => female
[submit] => Submit
)HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>Array
(
[name] => Dilan
[email] => dilan@example.com
[website] =>
[comment] => Hello!
[gender] => female
[submit] => Submit
)
</pre>Now Dilan leaves e-mail, website and comment empty and chooses no gender:
ئێستا دیلان ، و تێبینی بەتاڵ بەجێدەهێڵێت و هیچ ڕەگەزێک هەڵنابژێرێت:
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:
websitearrives as an empty string"". - A radio group with nothing chosen is not sent at all: there is no
genderkey. Reading$_POST["gender"]would give a warning; check it withisset()orempty()(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 sendssubmit => Submit(itsvalue), which is how PHP knows the form was sent. - All values arrive as strings, even numbers: an age field sends
"20", not20.
- و هەمیشە دەنێردرێن، تەنانەت ئەگەر بەتاڵیش بن:
websiteوەک بەتاڵ""دەگات. - کۆمەڵە کە هیچیان هەڵنەبژێردرابێت هەر نانێردرێت:
genderنییە. خوێندنەوەی$_POST["gender"]دەدات؛ بەisset()یانempty()بیپشکنە (وانەی داهاتوو). - نیشانە نەکراویش نانێردرێت؛ ئەوەی نیشانە کرابێت
valueـەکەی دەنێرێت. - تەنها کاتێک دەنێردرێت کە
nameـی هەبێت. لێرەداsubmit => Submitدەنێرێت (valueـەکەی) و PHP بەمە دەزانێت نێردراوە. - هەموو وەک دەگەن، تەنانەت ژمارەکانیش: تەمەن
"20"دەنێرێت، نەک20.
Lab 5: your first form-handling projectتاقیگەی ٥: یەکەم پرۆژەی مامەڵەکردن لەگەڵ فۆرم
- 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 folderC:\xampp\htdocs\form_handlingand open it in any editor.پرۆژەیەکی نوێی PHP لە دەستکاریکەرەکەتدا بکەرەوە و ناوی بنێ
form_handling. تاقیگەکە NetBeans بەکار دەهێنێت؛ بە XAMPP دەتوانیت بە سادەییC:\xampp\htdocs\form_handlingدروست بکەیت و لە هەر دەستکاریکەرێکدا بیکەیتەوە. - Write the formفۆرمەکە بنووسە
Write the form in
index.php(Example 1 below).لە
index.phpدا بنووسە (نموونەی ١ی خوارەوە). - Write the receiving pageلاپەڕەی وەرگر بنووسە
Create a second file,
welcome.php, that reads the value and prints it.دووەم،
welcome.php، دروست بکە کە بخوێنێتەوە و چاپی بکات. - 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 نێردراوە
<form action="welcome.php" method="post">
Email: <input type="text" name="uemail" placeholder="email address">
</form>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<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.phpwith 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"]. دواتر ڕێنماییەکی خۆڵەمێشی لەناو سندووقە بەتاڵەکەدا پیشان دەدات؛ ئەو ڕێنماییە وەک نانێردرێت.
<?php
echo "your email address is ".$_POST["uemail"];your email address is hana@example.com
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The dot
.joins the text and the value (concatenation).خاڵ
.دەق و پێکەوە دەلکێنێت.
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ـی تێدایە دەتوانێت ?>ی کۆتایی نەنووسێت؛ ئەمە تەنانەت پێشنیاریش دەکرێت، چونکە هیچ شتێک بە هەڵە لە دوایەوە چاپ نابێت.
<?php
$email = $_POST["uemail"] ?? "";
echo "Your email address is " . htmlspecialchars($email);Your email address is hana@example.com
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 پیشان نادات. ئەمە بەشە ونبووەکەیە: تەنها دەگۆڕێت.
<form action="welcome.php" method="get">
Email: <input type="text" name="uemail" placeholder="email address">
</form><?php
$email = $_GET["uemail"] ?? "";
echo "Your email address is " . htmlspecialchars($email);Your email address is hana@example.com
Common mistakesهەڵە باوەکان
- Forgetting
name.<input type="text">withoutnameis 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 withoutmethoduses 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 keywarnings. Useisset()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 withoutecho. - 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ڕاهێنان
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وەڵامەکە پیشان بدە
<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>";
}
?>First name: [ ] Last name: [ ] [Send] Hello, Rezan Ahmed!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<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>
<p>Hello, Rezan Ahmed!</p>The boxes are empty again: this form is not sticky yet (see sticky forms).
سندووقەکان دووبارە بەتاڵن: ئەم هێشتا لکاو نییە (بڕوانە فۆرمە لکاوەکان).
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=Erbilcan 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=3is 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 دەتوانێت تەنها بە سەردانکردنی کارا ببێت.
This form should send the user's city to city.php, which prints it. Nothing arrives. Find three mistakes.
ئەم دەبێت شاری بنێرێت بۆ city.php، کە چاپی دەکات. هیچ ناگات. سێ هەڵە بدۆزەوە.
<form action="city.php" method="post">
City: <input type="text">
<input type="submit">
</form><?php
echo "Your city is " . $_GET["City"];Show answerوەڵامەکە پیشان بدە
- The input has no
name, so nothing is sent: addname="city". - The form uses POST but the PHP reads
$_GET: use$_POST. - The key
"City"does not matchname="city"(case): use"city". Also add?? ""andhtmlspecialchars().
-
nameـی نییە، بۆیە هیچ نانێردرێت:name="city"زیاد بکە. - POST بەکار دەهێنێت بەڵام PHPـەکە
$_GETدەخوێنێتەوە:$_POSTبەکاربهێنە. -
"City"لەگەڵname="city"ناگونجێت (قەبارەی پیت):"city"بەکاربهێنە. هەروەها?? ""وhtmlspecialchars()زیاد بکە.
<?php
$city = $_POST["city"] ?? "";
echo "Your city is " . htmlspecialchars($city);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
actionsays where the data goes,methodsays how, and each field'snamebecomes 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="", orecho htmlspecialchars($_SERVER["PHP_SELF"])— never rawPHP_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 withhtmlspecialchars().
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
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زاراوە سەرەکییەکان
Form Validationپشتڕاستکردنەوەی فۆرم
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.