15 - Php & HTML Forms
15 - Php & HTML Forms
INTERNET PROGRAMMING
AND E-APPLICATIONS
Lecture 15: PHP & HTML Forms
Forms
• Web applications often use forms to collect data
from users.
• The following slides demonstrate how forms are
used for this purpose.
A Simple HTML Form
• The example below displays a simple HTML form with two input fields and a submit
button:
<html>
<body>
<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>
</body>
</html>
• When the user fills out the form above and clicks the submit button, the form data is
sent for processing to a PHP file named "welcome.php". The form data is sent with the
HTTP POST method.
• To display the submitted data you could simply echo all the variables.
• The following slide shows how the "welcome.php“ file would look like.
welcome.php
<html>
<body>
</body>
</html>
The GET method
The same result could also be achieved using the HTTP GET method:
<html>
<body>
</body>
</html>
And the following slide shows how the "welcome_get.php“ file would look like.
welcome_get.php
<html>
<body>
</body>
</html>
GET vs. POST
• Both GET and POST create an array (e.g. array( key => value,
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.
• Both GET and POST are treated as $_GET and $_POST. These
are superglobals, which means that they are always
accessible, regardless of scope - and you can access them
from any function, class or file without having to do
anything special.
• $_GET is an array of variables passed to the current script
via the URL parameters.
• $_POST is an array of variables passed to the current script
via the HTTP POST method.
Another GET Example
Assume we have an HTML page that contains a hyperlink with parameters:
<html>
<body>
</body>
</html>
•When a user clicks on the link "Click Here", the parameters "reg“, "dis"
and "wd" are sent to "get_example.php", and you can then access their
values in "get_example.php" with $_GET.
•The next slide shows sample code in "get_example.php“.
get_example.php
<html>
<body>
<?php
echo "Region: " . $_GET['reg'] . "<br/> District: " .
$_GET['dis'] . "<br/> Ward: " . $_GET['wd'];
?>
</body>
</html>
When to use GET?
• 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. However, because the
variables are displayed in the URL, it is possible to
bookmark the page. This can be useful in some
cases.
• GET may be used for sending non-sensitive data.
• GET should NEVER be used for sending passwords
or other sensitive information!
When to use POST?
• 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)
and has no limits on the amount of information to
send.
• Moreover POST supports advanced functionality
such as support for multi-part binary input while
uploading files to server.
• However, because the variables are not displayed in
the URL, it is not possible to bookmark the page.
Form and PHP Script in the Same File
You can place the PHP script in the same file as the form, as shown in this example:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
First Name: <input type="text" name="fname">
Surname: <input type="text" name="sname">
<input type="submit" value="Click to Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { // 1st option
// OR if (isset($_POST['fname']) && isset($_POST['sname'])){ // 2nd option
// OR if (isset($_POST['submit'])){ // 3rd option
$fn = $_REQUEST['fname']; // OR $fn = $_POST['fname'];
$sn = $_REQUEST['sname']; // OR $sn = $_POST['sname'];
echo 'Full Name: '.$fn.' '.$sn ;
}
?>
</body>
</html>
Form and PHP Script in the Same File
If you use the 3rd option, the name attribute (i.e. attribute name
as well as attribute value) should be added in the submit code
above, i.e. it should look like:
End