Comprehensive Guide to PHP Forms: Methods, Superglobals, and Data Retrieval
Learn about PHP form handling including action and method attributes, GET vs POST methods, superglobals, and retrieving data from radio buttons, checkboxes, and dropdown lists.
Comprehensive Guide to PHP Forms: Methods, Superglobals, and Data Retrieval
1.
PHP Form
Prepared By:-
KaleJ. N.
Assistant Professor,
Computer Engineering,
Sanjivani College of Engineering, Kopargaon
Prepared By: Kale J. N.
2.
action attribute:-
The actionattribute specifies where the form data will be sent after
the user clicks the submit button. It tells the browser which page or
script will process the form data
Prepared By: Kale J. N.
3.
<form action="welcome.php"
method="post">
Name:
<input type="text"name="name">
<input type="submit">
</form>
• //When the user clicks Submit:
• //The form data goes to welcome.php
•
• If action="" then Then the form
submits to the same page.
Prepared By: Kale J. N.
4.
Method attribute
• Themethod attribute specifies
how the form data will be sent
to the server.
• There are two main methods:
• GET
• POST
Prepared By: Kale J. N.
5.
GET Method
<form action="welcome.php"
method="get">
Name:
<inputtype="text"
name="name">
<input type="submit">
</form>
//After submission, URL becomes:
//welcome.php?name=sai
• In GET method, form data is
sent through the URL.
• Characteristics of GET
• Data is visible in URL
• Limited data size
• Less secure
Prepared By: Kale J. N.
6.
POST Method
<form action="welcome.php"
method="post">
Name:
<inputtype="text"
name="name">
<input type="submit">
</form>
//URL will look like:
//welcome.php
• In POST method, form data is
sent inside the HTTP request
body, not visible in URL.
• Data is hidden.
Prepared By: Kale J. N.
7.
Difference Between GETand POST
Feature GET POST
Data visibility Visible in URL Hidden
Security Less secure More secure
Data size Limited Large
Usage Search forms Login, registration
Prepared By: Kale J. N.
8.
Superglobals
• PHP provides9 superglobal
variables:
• $GLOBALS
• $_SERVER
• $_REQUEST
• $_POST
• $_GET
• $_FILES
• $_ENV
• $_COOKIE
• $_SESSION
Prepared By: Kale J. N.
• Superglobals are built-in PHP
variables that are available in all
scopes of a PHP script.
• This means:
• They can be accessed anywhere in
the program
9.
$_GET Superglobal
• Definition
•$_GET is used to collect form
data sent using the GET
method.
• The data is visible in the URL.
<?php
$name = $_GET['name'];
echo "Hello " . $name;
?>
Prepared By: Kale J. N.
10.
$_POST Superglobal
• Definition
•$_POST is used to collect form
data sent using POST method.
• The data is not visible in the
URL.
<?php
$name = $_POST['name'];
echo "Hello " . $name;
?>
Prepared By: Kale J. N.
11.
Retrieving Data (RadioButton)
<input type="radio"
name="gender" value="Male">
Male
<input type="radio"
name="gender" value="Female">
Female
$gender = $_POST['gender'];
Prepared By: Kale J. N.
12.
Retrieving Data (Checkbox)
<input type="checkbox"
name="hobby[]"
value="Reading"> Reading
<input type="checkbox"
name="hobby[]" value="Music">
Music
$hobby = $_POST['hobby'];
foreach($hobby as $h)
{
echo $h;
}
Prepared By: Kale J. N.
13.
Retrieving Data (Dropdown List)
<select name="city">
<option
value="Nashik">Nashik</option>
<option
value="Pune">Pune</option>
</select>
$hobby = $_POST['hobby'];
foreach($hobby as $h)
{
echo $h;
}
Prepared By: Kale J. N.
$city = $_POST['city'];