0% found this document useful (0 votes)
33 views48 pages

PHP Unit 4 Updated (1) - 1

Uploaded by

Yashasvi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views48 pages

PHP Unit 4 Updated (1) - 1

Uploaded by

Yashasvi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Web Based Application

Development with PHP


Unit IV: Creating and Validating Forms
⮚ Creating a webpage using GUI components
▪ GUI is an interface that allows users to interact with different electronic devices using icons and
other visual indicators. The graphical user interfaces were created because command line
interfaces were quite complicated and it was difficult to learn all the commands in it.
▪ In today’s times, graphical user interfaces are used in many devices such as mobiles, MP3
players, gaming devices, smartphones etc.
▪ The below diagram provides the position of the graphical user interface with respect to the
computer system −
Unit IV: Creating and Validating Forms
⮚ Creating a webpage using GUI
components
Unit IV: Creating and Validating Forms
⮚ Creating a webpage using GUI components
⮚ Elements in Graphical User Interface:
⮚ Graphical User Interface makes use of visual elements mostly. These elements define the
appearance of the GUI. Some of these are described in detail as follows −
► Window
► This is the element that displays the information on the screen. It is very easy to manipulate a
window.
► It can be opened or closed with the click of an icon. Moreover, it can be moved to any area by
dragging it around.
► In a multitasking environment, multiple windows can be open at the same time, all of them
performing different tasks.
► There are multiple types of windows in a graphical user interface, such as container window,
browser window, text terminal window, child window, message window etc.
Unit IV: Creating and Validating Forms
⮚ Creating a webpage using GUI components
► Menu
► A menu contains a list a choices and it allows users to select one from them. A menu bar is
displayed horizontally across the screen such as pull down menu.
► When any option is clicked in this menu, then the pull down menu appears.
► Another type of menu is the context menu that appears only when the user performs a specific
action.
► An example of this is pressing the right mouse button. When this is done, a menu will appear
under the cursor.
Unit IV: Creating and Validating Forms
⮚ Creating a webpage using GUI components
► Controls
► Information in an application can be directly read or influences using the graphical control
elements.
► These are also known as widgets. Normally, widgets are used to display lists of similar items,
navigate the system using links, tabs etc. and manipulating data using check boxes, radio boxes
etc.
► Tabs
► A tab is associated with a view pane. It usually contains a text label or a graphical icon. Tabs are
sometimes related to widgets and multiple tabs allow users to switch between different widgets.
► Tabs are used in various web browsers such as Internet Explorer, Firefox, Opera, Safari etc.
Multiple web pages can be opened in a web browser and users can switch between them using
tabs.
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post Methods
► PHP provides two methods through which a client (browser) can send information to the server.
These methods are given below, and discussed in detail:
► GET method
► POST method
► Get and Post methods are the HTTP request methods used inside the <form> tag to send form
data to the server.
► HTTP protocol enables the communication between the client and the server where a browser
can be the client, and an application running on a computer system that hosts your website can
be the server.
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post Methods
⮚ GET method
► The GET method is used to submit the HTML form data. This data is collected by the predefined
$_GET variable for processing.
► The information sent from an HTML form using the GET method is visible to everyone in the
browser's address bar, which means that all the variable names and their values will be
displayed in the URL. Therefore, the get method is not secured to send sensitive information.
► This is the built-in PHP super global array variable that is used to get values submitted via HTTP
GET method.
► Data in GET method is sent as URL parameters that are usually strings of name and value pairs
separated by ampersands (&).
► URL with GET data looks like this:
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post Method
⮚ Advantages of using the GET Method :
• Since the data sent by the GET method are displayed in the URL, it is possible to bookmark the
page with Specific query string values.
• GET requests can be cached and GET requests to remain in the browser history. GET requests
can be bookmarked.
⮚ Disadvantages of using the GET Method
• The GET method is not suitable for passing sensitive information such as the username and
password because these are fully visible in the URL query string as well as potentially stored in
the client browser memory as a visited page.
• Because the GET method assigns data to a server environment variable, the length of the URL is
limited. So, there is a limitation for the total data to be sent.
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post
Method
► GET Method :
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post Method
► Post Method :
► Similar to the GET method, the POST method is also used to submit the HTML form data. But
the data submitted by this method is collected by the predefined superglobal variable $_POST
instead of $_GET.
► Unlike the GET method, it does not have a limit on the amount of information to be sent. The
information sent from an HTML form using the POST method is not visible to anyone.
► This is the built-in PHP super global array variable that is used to get values submitted via the
HTTP POST method. Example of post method is given below:
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post
Method
► Post Method :
Unit IV: Creating and Validating Forms
⮚ Browser Role: Get and Post Method
⮚ Advantages of using the POST Method :
• It is more secure than GET because user-entered Information is never visible in the URL query
string or in the server logs.
• There is a much larger limit on the amount of data that can be passed and one can send text
data as well as binary data (uploading a file) using POST.
⮚ Disadvantages of using the POST Method
• Since the data sent by the POST method is not visible in the URL, so it is not possible to
bookmark the page with a specific query.
• POST requests are never cached.
• POST requests do not remain in the browser history.
Unit IV: Creating and Validating Forms
⮚ Server Role:
⮚ $_SERVER:
⮚ It is a PHP super global variable that stores the information about headers, paths and script
locations. Some of these elements are used to get the information from the super global variable
$_SERVER.
⮚ A web server entries the information in this array that is accessible through the
$_SERVER variable. It's automatically determined by PHP based on the server configuration and
the current HTTP request. This array is accessible from any part of your script, making it a
convenient resource for retrieving important details.
⮚ Here, key is a placeholder for the specific element you want to access.
Unit IV: Creating and Validating Forms
⮚ Server
Role:
⮚ $_SERVER:
Unit IV: Creating and Validating Forms
⮚ Server Role:
⮚ $_SERVER:
⮚ Below program illustrates the use of $_SERVER in PHP:
Unit IV: Creating and Validating Forms
⮚ Form Controls:
⮚ Form processing contains a set of controls through which the client and server can communicate
and share information. The controls used in forms are:
⮚ Textbox: Textbox allows the user to provide single-line input, which can be used for getting
values such as names, search menu and etc.
⮚ Textarea: Textarea allows the user to provide multi-line input, which can be used for getting
values such as an
⮚ Radio Buttons: Radio buttons allow the user to select only one option from the given set of
options.
⮚ CheckBox: Checkbox allows the user to select multiple options from the set of given options.
⮚ Buttons: Buttons are the clickable controls that can be used to submit the form. address,
message etc.
Unit IV: Creating and Validating Forms
⮚ Form
Controls:
Unit IV: Creating and Validating Forms
⮚ Form Controls: (with PHP
code)
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:

• A web page having multiple forms: A web page having multiple forms can be processed in two
types:

1. Posting each form to different PHP script file for processing

1. Multiple functionalities can be provided on a single web page by providing multiple forms on a
web page having different functionality.

2. Each form on this web page will be given a separate name that will uniquely identify the form
on a web page with multiple forms.

3. Data from each form should be given to separate PHP script file for processing by specifying
PHP script filename in the action attribute of the forms.
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:

• A web page having multiple forms: A web page having multiple forms can be processed in two
types:

1. Posting each form to different PHP script file for processing

4. Each PHP Script should be written in such a fashion that will handle all the data coming from
that form.

5. A disadvantage of this method is that we have to write a separate PHP script for each form,
which creates extra files for handing
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:( code with
output)
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms: (code with
output)
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:
⮚ 2.Posting all form to single PHP script file for processing
1. Multiple functionalities can be provided on a single web page by providing multiple forms on a web page
having different functionality.

2. Each form on this web page will be given a separate name that will uniquely identify the form on a web
page with multiple forms.

3. Data from each form should be given to a single PHP script file for processing by specifying PHP script
filename in the action attribute of the forms.

4. Each PHP Script should be written in such a fashion that it will handle all the data coming from multiple
forms.

5. Data from multiple forms can be identified by it submit button and the processing each form will be written
with help of if, else, and else if conditional statements.

6. The advantage of this method is that we have to write a single PHP script for processing of all forms,
which saves time in the creation and handling of extra files.
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:
⮚ 2.Posting all form to single PHP script file for
processing
A Form having Multiple Submit
Buttons
● Multiple operations can be provided on a single form by providing
a different buttons for different operation.
● Based on which button is clicked, data in the form is processed
differently for the operations mentioned on that button.
● Identification of the button is done by its name on the server and
corresponding operation is called with the help of if,else and else
if conditional statements.
Example: multibutton.html
<html>
<head>
<title> Multiple form Demo</title>
</head>
<body>
<form name= “mailform” methode= “post” action= “filename.php”>

<input type= “text” name= “no1” id= “no1”/>


<input type= “text” name= “no2” id= “no2”/>

<input type= “submit” name= “addbtn” value= “Add”/>


<input type= “submit” name= “subbtn” value= “Sub”/>
</form>
</body>
</html>
Output:
phpmultibuttondemo.php
<?php
if($_SERVER[‘REQUEST_METHOD’]== ‘POST’) {
if(isset($_POST [‘addbtn’])) {
echo “Addition of these two numbers is : ” . ((int) $_POST[‘no1’] +
(int)$_POST[‘no2’]);
}
else if(isset($_POST[‘subbtn’])) {

echo “Subtraction of these two numbers is : ” . ((int) $_POST[‘no1’] -


(int) $_POST[‘no2’]);
}
}
?>
Output:
4.4 Web Page Validation
● User may by mistakenly submit the data through form with empty fields or in
wrong format.
● PHP script must ensure that required fields are complete and submitted data is in
valid format.
● PHP provides some inbuilt function using these functions that input data be
validated.
● empty() function will ensure that text field is not blank it is with some data,
function accepts a variable as an argument and returns TRUE when the text field
is submitted with empty string,zero,NULL or FALSE value.
● Is_numeric() function will ensure that data entered in a text field is a numeric
value, the function accepts a variable as an argument and returns TRUE when the
text field is submitted with numeric value.
● preg_match() function is specifically used to performed validation for entering text
in the text field, function accepts a “regular expression” argument and a variable
as an argument which has to be in a specific pattern.
● Typically it is for validating email,IP address and pin code in a form.
Example : Formvalidation
<html>
<head>
<title> Validating Form Data </title>
</head>
<body>

<form method= “post” action= “validdata.php”>


Name:<input type= “text” name= “name” id= “name”/><br/>

Mobile Number:<input type= “text” name= “mobileno” id= “mobileno”/><br/>

Email ID:<input type= “text” name= “email” id= “email”/><br/>

<input type= “submit” name= “submit_btn” value= “submit”/>


</form>
</body>
</html>
Output:
Example valid data.php
<?php
if($_SERVER[‘REQUEST_METHODE’]== ‘POST’) {
if(empty($_POST[‘name’])
{
echo “Name can’t be blank <br/>”;
}
if(!is_numeric($_POST[‘mobileno’]))
{
echo “Enter valid Mobile Number </br>”;
}
$pattern= ‘\b[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}\b’;
if(!preg_match($pattern,$_POST[‘email’]))
{
echo “Enter valid Email ID.<br/>”;
}
}

?>
4.5 Cookies
What is a Cookie?
PHP Cookies is a small piece of information which is stored at client browser.it is used to
recognize the user.
Cookies is created at server side and saved to client browser.
Each time when client sends request to the server, cookie is embedded with request.
Such way, cookie can be received at the server side.
In short cookie can be created,send and received at server end.

A cookie is often used to identify a user. A cookie is a small file that the server embeds
on the user's computer. Each time the same computer requests a page with a browser, it
will send the cookie too. With PHP, you can both create and retrieve cookie values.
Create Cookies With PHP
A cookie is created with the setcookie() function.

Syntax
setcookie(name, value, expire, path, domain, secure,
httponly);
PHP Create/Retrieve a Cookie

The following example creates a cookie named "user" with the value "John Doe".
The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie
is available in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie "user" (using the global variable
$_COOKIE). We also use the isset() function to find out if the cookie is set:
Example
Modify a Cookie Value
To modify a cookie, just set (again) the cookie using the setcookie() function:

Example
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the
past:

Example
PHP - $_SESSION
One of the superglobal variables in PHP, $_SESSION is an associative array of session
variables available in the current script. $HTTP_SESSION_VARS also contains the
same information, but it is not a superglobal.

What is a Session?
● Session are used to store important information such as the user ID more securely on the
server where malicious users cannot temper with them.
● To pass values from one page to another.
● Sessions are the alternative to cookies on browsers that do not support cookies.
● You want to store global variables in an efficient and more secure way compared to
passing them in the URL.
● You are developing an application such as a shopping cart that has to temporary store
information with a capacity larger than 4KB.
The session_start() Function

In order to enable access to session data, the session_start() function must


be invoked. session_start() creates a session or resumes the current one
based on a session identifier passed via a GET or POST request, or passed
via a cookie.
session_start(array $options = []): bool

This function returns true if a session was successfully started, else it


returns false.
Handling Session Variables
To create a new session variable, add a key-value pair in the $_SESSION array −
$_SESSION[ "var"]=value;

To read back the value of a session variable, you can use echo/print statements, or var_dump() or
print_r() functions.
echo $_SESSION[ "var"];

To obtain the list of all the session variables in the current session, you can use a foreach loop to
traverse the $_SESSION −
foreach ($_SESSION as $key=>$val)
echo $key . "=>" . $val;

To manually clear all the session data, there is session_destroy() function. A specific session variable
may also be released by calling the unset() function.
unset($_SESSION[ "var"]);
Example
The following PHP script renders an HTML form. The form data
is used to create three session variables. A hyperlink takes the
browser to another page, which reads back the session
variables.
Save this code as "test.php" in the document root folder, and
open it in a client browser. Enter the data and press the
Submit button.
When you click the "Submit" button, it will show a list of
all the session variables created −

You might also like