PHP form handling
Form
• A Document that containing blank fields, that
the user can fill the data or user can select the
data.
• It is like interface to send data to web server
• There are two ways the browser client can
send information to the web server.
The GET Method
The POST Method
GET and POST
• Both GET and POST create an array
e.g. array( key1 => value1, key2 => value2,
key3 => value3, ...)
• This array holds key/value pairs, where keys
are the names of the form controls and values
are the input data from the user.
• The respective arrays are
$_GET and $_POST (built in arrays)
The GET Method
• Information sent from a form with the GET
method is visible to everyone (all variable
names and values are displayed in the URL).
• GET also has limits on the amount of
information to send. The limitation is about
2000 characters.
• GET may be used for sending non-sensitive data.
• Note: GET should NEVER be used for sending
passwords or other sensitive information!
The POST Method
• Information sent from a form with the POST method
is invisible to others (all names/values are
embedded within the body of the HTTP request)
• Has no limits on the amount of information to send.
• However, because the variables are not displayed in
the URL, it is not possible to bookmark the page.
• Developers prefer POST for sending form data.
Example (read and display form data)
• A form (name and age data)
<html>
<body>
<form action =“welcome.php” method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
PHP code (welcome.php)
<html>
<body>
<?php
$a=$_POST['name'];
$b=$_POST[‘age'];
echo "Welcome ".$a"<br />";
echo "You are ". $b. " years old.";
?>
</body>
</html>