Professional Documents
Culture Documents
Development (IWD)(4340704)
Made by:
Hardik N. Talsania (Lecturer – RCTI, Ahmedabad)
Contents
Learning Outcomes
User-defined Input in PHP?
Processing input data
GET v/s POST
HTML Integration with PHP on same page
Redirecting the user
PHP Superglobals
However, we can accept data from the user through HTML Form Input
Elements and process it through PHP code.
‘name’ attribute of the HTML form element is used to pass values to the server
side (PHP).
NOTE: Without a ‘name’ attribute, value that is passed from HTML side is NOT
accessible on server side (PHP).
4 H N TALSANIA (RCTI) Sunday, July 2, 2023
User-defined Input in PHP?
Syntax:
<input type=‘inputtype’ name=‘elementname’>
Example:
<html> <body>
<form action=‘myphppage.php’>
Your Full Name: <input type=‘text’ name=‘txtfullname’ >
<input type=‘submit’>
</form>
</body></html>
For that, we need to set ‘method’ attribute of the HTML form to either ‘get’ or
‘post’ respectively. Default method is ‘get’.
Alternatively, $_REQUEST can be used to access the passed values for any of
the above (‘get’ or ‘post’) methods.
Since, $_GET, $_POST and $_REQUEST all are arrays, values are accessed by
passing HTML element name as keys to these arrays.
6 H N TALSANIA (RCTI) Sunday, July 2, 2023
Processing Input Data
Syntax:
$_GET[‘htmlelementname’] or $_POST[‘htmlelementname’] or
$_REQUEST[‘htmlelementname’]
Full Example:
HTML Code:
<html> <body>
<form action=‘myphppage.php’ method=‘post’>
Your Full Name: <input type=‘text’ name=‘txtfullname’ >
<input type=‘submit’>
</form>
</body></html>
NOTE: $_REQUEST can be used with any method ‘get’ or ‘post’. However, $_GET
can be used with only ‘get’ method and $_POST can be used only with ‘post’
method.
3 Limitation on the length of the values: No limitation on the length of the values
Generally 255 characters
Better (faster) performance, as the values Lower (slower) performance as the time is spent
4 are simply appended in the URL (header) in including POST values in the HTTP body
5 Supports only string data types Supports string, numeric, binary, etc. data types
6 GET results can be bookmarked POST results cannot be bookmarked
7 GET request is often cacheable POST request is hardly cacheable
8 Parameters remain in web browser history Parameters are not saved in browser history
9 Reloading the page is harmless Data will be re-submitted on reloading the page
However, it should be done carefully, as PHP errors may occur, if the passed
values are NOT found when the page is loaded for the first time.
In such cases, passed values should be checked for availability by using isset( )
function, before they can be used.
NOTE: PHP code can be written anywhere (any no. of times) before / after /
between the HTML code between starting and closing PHP tags (<?php and ?>).
For e.g. user can be redirected to ‘Home’ page (home.php), upon successful
login attempt (after verifying user’s identity).
For e.g., User can be redirected to Login page (login.php) after 5 seconds. Till
that time, a message can be shown.
Example:
header(‘refresh: 5; url=login.php’ );
echo ‘You will be redirected to Login page in 5 seconds’;
14 H N TALSANIA (RCTI) Sunday, July 2, 2023
PHP Superglobals
PHP provides many inbuilt superglobal arrays which can be used to obtain
some required information as follows:
Sr. No. Superglobal Usage
1 $GLOBALS Accessing global variables from anywhere in the PHP script
2 $_SERVER Accessing information about headers, paths & script locations
3 $_REQUEST Accessing data after submitting an HTML form
4 $_GET Accessing data after submitting an HTML form with method=‘get’
5 $_POST Accessing data after submitting an HTML form with method=‘post’
6 $_FILES Accessing data related to the file(s) selected for uploading
7 $_ENV Accessing information about environment variables
8 $_COOKIE Accessing information about cookies stored on the system
9 $_SESSION Setting or getting information about a session variable