You are on page 1of 16

Processing User Data – Introduction to Webpage

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

2 H N TALSANIA (RCTI) Sunday, July 2, 2023


Learning Outcomes
After completion of this session, students will be able to:

Create interactive web pages in PHP by inputting through HTML forms


Differentiate between GET & POST methods
Make use of various PHP Superglobals in PHP

3 H N TALSANIA (RCTI) Sunday, July 2, 2023


User-defined Input in PHP?
As such, there is NO facility or any in-built function in PHP to accept input from
the user.

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>

5 H N TALSANIA (RCTI) Sunday, July 2, 2023


Processing Input Data
On the server side (PHP), the passed value can be obtained through $_GET or
$_POST super global variables.

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>

7 H N TALSANIA (RCTI) Sunday, July 2, 2023


Processing Input Data
Full Example (continued):
PHP Code:
<?php
echo ‘Hello ‘ . $_POST[‘txtfullname’];
?>

 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.

8 H N TALSANIA (RCTI) Sunday, July 2, 2023


Processing Input Data
Some examples of passing values through HTML Form Elements:
Sr.
Element HTML Code PHP Code
No.
Multi-line
1 <textarea name=‘address’ rows=‘5’ cols=‘20’></textarea> echo $_GET[‘address’];
Text Box
2 Radio <input type=‘radio’ name=‘gender’ value=‘Male’> MALE echo $_GET[‘gender’];
Button <input type=‘radio’ name=‘gender’ value=‘Female’> FEMALE
<input type=‘checkbox’ name=‘hobby[]’ value=‘Reading’> Reading
3 Checkbox <input type=‘checkbox’ name=‘hobby[]’ value=‘Dancing’> Dancing print_r($_GET[‘hobby’]);
<input type=‘checkbox’ name=‘hobby[]’ value=‘Singing’> Singing
4 Drop Down <select name=‘state’> … </select> echo $_GET[‘state’];
5 List Box <select name=‘city[]’ multiple> … </select> print_r($_GET[‘city’]);
6 Hidden Field <input type=‘hidden’ name=‘batchid’ /> echo $_GET[‘batchid’];

9 H N TALSANIA (RCTI) Sunday, July 2, 2023


GET v/s POST
Sr. No. GET Method POST Method
1 Passed values are visible in the URL Passed values are not visible in the URL
2 Data is not secure Data is secure

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

10 H N TALSANIA (RCTI) Sunday, July 2, 2023


HTML Integration with PHP on same page
Important: HTML and PHP codes can be combined on a single page. That page
should have .php extension.

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 ?>).

11 H N TALSANIA (RCTI) Sunday, July 2, 2023


HTML Integration with PHP on same page
Example (myphppage.php):
<html> <body>
<?php
if(isset($_POST[‘txtfullname’])) {
echo ‘Hello ‘ . $_POST[‘txtfullname’];
}
?>
<form action=‘myphppage.php’ method=‘post’>
Your Full Name: <input type=‘text’ name=‘txtfullname’ >
<input type=‘submit’>
</form>
</body></html>
12 H N TALSANIA (RCTI) Sunday, July 2, 2023
Redirecting the user
User can be dynamically redirected to another page (HTML or PHP) directly or
when some predefined condition is met.

For e.g. user can be redirected to ‘Home’ page (home.php), upon successful
login attempt (after verifying user’s identity).

PHP header( ) function is used to dynamically redirect to a different page.

Syntax: header(‘Location: pagename’);

Example: header(‘Location: home.php’); //Redirect user to ‘home.php’ page.

13 H N TALSANIA (RCTI) Sunday, July 2, 2023


Redirecting the user
Important: User can also be redirected to another page, after some specific
time has been passed.

For e.g., User can be redirected to Login page (login.php) after 5 seconds. Till
that time, a message can be shown.

Syntax: header(‘refresh: time_in_seconds; url=pagename’ );

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

15 H N TALSANIA (RCTI) Sunday, July 2, 2023


THANK YOU

16 H N TALSANIA (RCTI) Sunday, July 2, 2023

You might also like