0% found this document useful (0 votes)
19 views

PHP Chapter 4

The document discusses various form controls and methods for working with multiple forms and validating form data in PHP. It covers hidden form controls, submitting multiple forms to different or same PHP files, and using single forms with multiple submit buttons. It also discusses validating form input data using built-in PHP functions like empty().

Uploaded by

sarcarzam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

PHP Chapter 4

The document discusses various form controls and methods for working with multiple forms and validating form data in PHP. It covers hidden form controls, submitting multiple forms to different or same PHP files, and using single forms with multiple submit buttons. It also discusses validating form input data using built-in PHP functions like empty().

Uploaded by

sarcarzam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Creating and Validating

Forms
Unit - IV

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


1
MHSSP
4.1 GUI Components

• HTML controls like textbox, textarea, check boxes, radio buttons etc
enclosed in HTML form are used to collect data from user.

• When the user fills out the form and clicks submit button, the form
data is sent for processing to a PHP file.

• There are two methods “get” and “post” commonly used to send data
from HTML controls to PHP script on server.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


2
MHSSP
4.1 GUI Components
• About 10 elements are commonly found within <form/>
elements.
• Text Box
• Password
• Text Area
• Check Box
• Radio Button
• Drop Down List
• List Box

• Standard Button
• Submit Button
• Reset Button Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
3
MHSSP
4.1 GUI Components

• <form> tag is used to create an HTML form for user input.


• Name: Specifies the name of a form.
• Action: URL
specifies where to send the form data when a form is submitted.
• Target: Specifies where to display the response that is received after
submitting the form.
_blank , _self , _parent , _top
• Method: Specifies the HTTP method to use when sending form data.
Get :
Post :

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


4
MHSSP
4.1 GUI Components
• Get:
• It is the default method when submitting a form.

• When Get method is used, the submitted form data will be visible in the web
page address bar.

• It appends form data into the URL in name/value pair separated by &.

• The length of a URL is limited.

• Never use Get to send sensitive data.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


5
MHSSP
4.1 GUI Components
• Get: GUI.html file Welcome.php file
<html> <body> <html>
<form action="welcome.php" <body>
method=“get"> Welcome <?php echo
Name: <input type="text" $_GET["name"]; ?><br>
name="name"><br> Your email address is: <?php
E-mail: <input type="text" echo $_GET["email"]; ?>
name="email"><br> </body>
<input type="submit"> </html>
</form>
</body> </html>
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
6
MHSSP
4.1 GUI Components
• Get:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


7
MHSSP
4.1 GUI Components
• Post:
• Post method is used if the form contains personal or sensitive information.

• The POST method does not display the submitted form data in the page
address field.

• POST has no size limitations, and can be used to send large amounts of data.

• Form submissions with POST cannot be bookmarked.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


8
MHSSP
4.1 GUI Components
• Post: GUI.html file Welcome.php file
<html> <body> <html>
<form action="welcome.php" <body>
method="post"> Welcome <?php echo
Name: <input type="text" $_POST["name"]; ?><br>
name="name"><br> Your email address is: <?php
E-mail: <input type="text" echo $_POST["email"]; ?>
name="email"><br> </body>
<input type="submit"> </html>
</form>
</body> </html>
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
9
MHSSP
4.1 GUI Components
• Post:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


10
MHSSP
4.2 Form Controls

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


11
MHSSP
4.2 Form Controls

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


12
MHSSP
4.2 Form Controls
• Hidden Controls:

• Hidden controls are used to store the data in a webpage that user cant see.

• Hidden controls will be included in <form> element of a web page what will be
used to store data that will not be visible to the user, which will be sent to PHP
script available on the server.

• Data on the server will be fetched by any one of the method (GET or POST).

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


13
MHSSP
4.2 Form Controls
• Hidden Controls:
<form name="form1" method="post"
action="hidden_control1.php">
<input type="hidden"
name="userid" value="101">
<input type="submit" value="Submit">
</form>

<?php
if(isset($_POST["userid"]))
{
echo "User ID: ".$_POST["userid"];
}
?>

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


14
MHSSP
4.3 Working with multiple forms
• A web page with multiple forms:

A web page with multiple forms can be processed in 2 ways:

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

2. Posting all forms to single PHP script file for processing:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


15
MHSSP
4.3 Working with multiple forms
• Posting each form to different PHP script file for processing:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


16
MHSSP
4.3 Working with multiple forms
• Posting each form to different PHP script file for processing:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


17
MHSSP
4.3 Working with multiple forms
• Posting all forms to a single PHP script file for processing:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


18
MHSSP
4.3 Working with multiple forms
• Posting all forms to a single PHP script file for processing:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


19
MHSSP
4.3 Working with multiple forms
• Single form with multiple submit buttons:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


20
MHSSP
4.3 Working with multiple forms
• Single form with multiple submit buttons:

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


21
MHSSP
4.4 Web Page Validation
• PHP provides some inbuilt functions using these functions that input
data can be validated.

• empty( ):
• It ensures that text field is not blank.
• This 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( ):
• It ensures that data entered in a text field is a numeric value.
• Function accepts a variable as an argument and returns TRUE when the text
field is submitted with numeric value.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


22
MHSSP
Regular Expression
• A regular expression is an object that describes a pattern of characters.

• Regular expressions are used to perform pattern-matching and "search-and-


replace" functions on text.
Syntax:
/pattern/modifiers;

• E.g.: var patt = /w3schools/i


Where: w3schools/i is a regular expression
w3schools is a pattern to be used in search
i is a modifier (modifies search to be case-insensitive)

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


23
MHSSP
Regular Expression
• Modifiers:

• g : performs a global match, case-sensitive(find all matches rather than


stopping after the first match)

• i : performs case-insensitive matching and returns the first occurrence.

• m: By default, all matching or search operation is done as case sensitive and


on single line. To perform search or matching on text containing new line
character (\n) use modifier (m). Performs multiline matching.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


24
MHSSP
Regular Expression
• Brackets: they are used to find a range of characters.

• [abc] : find any character between the bracket. i.e. between a, b and c.

• [^abc] : find any character NOT between the brackets.

• [0-9]: Find any character between the brackets (any digit).

• (x|y): Find any of the alternatives specified.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


25
MHSSP
Regular Expression
• Metacharacters: Characters with special meaning.

• . : Find any single character, except newline or line terminator.

• \w : Find a word character.

• \W: Find a Non word character.

• \d : Find a digit.

• \D : Find a Non digit character.

• \s : Find a white space character.

• \S : Find a Non white space character.


• \b : Find a match at the beginning of a word: \bDICE
or at the end of the word: DICE\b

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


26
MHSSP
Regular Expression
• Quantifiers:
• n+ : Matches any string that contains at least one n. (1 or more)

• n* : Matches any string that contains zero or more occurrences of n.

• n?: Matches any string that contains zero or one occurrences of n.

• n{X} : Matches any string that contains a sequence of X n’s.

• n{X,Y} : Matches any string that contains a sequence of X to Y n’s.

• n{X,} : Matches any string that contains a sequence of at least X n’s.

• n$ : Matches any string with n at the end of it.

• ^n : Matches any string with n at the beginning of it.


Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
27
MHSSP
4.4 Web Page Validation
• preg_match( ):
• This function return whether a match was found in a string or not. Returns 1 if
the pattern was found and 0 if not.

Syntax: preg_match(pattern, input, matches, flags, offset)

Pattern: Required, contains a regular expression


Input: Required, the string in which the search will be performed.
Matches: Optional. The variable used in this parameter will be populated with an
array containing all of the matches that were found
Flags: Optional. A set of options that change how the matches array is
structured:
Offset: Optional. Defaults to 0. Indicates how far into the string to begin
searching. The preg_match() function will not find matches that occur before
the position given in this parameter

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


28
MHSSP
4.4 Web Page Validation
• preg_match_all( ):
• Returns the number of times the pattern was found in the string, which may
also be 0

Syntax: preg_match_all(pattern, input, matches, flags, offset)

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


29
MHSSP
4.4 Web Page Validation
• preg_replace( ):
• Returns a new string where matched patterns have been replaced with another
string

Syntax: preg_replace(pattern, replacements, input, limit, count)

Pattern: Required, contains a regular expression


Replacements: Required. A replacement string or an array of replacement strings
Input: Required, the string in which the search will be performed.
Limit: Optional. Defaults to -1, meaning unlimited. Sets a limit to how many
replacements can be done in each string.
Count: Optional, Indicates how many replacements were performed.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


30
MHSSP
4.5 Cookies
• Use of cookies:

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

• A cookie is often used to identify a user (sessions).

• Hence, securing a cookie effectively means securing a user's identity.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


31
MHSSP
4.5 Cookies
• Attributes of cookies:

• Name & Value


• Secure
• Domain
• Path
• HTTPOnly
• Expires

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


32
MHSSP
4.5 Cookies
• Attributes of cookies: Name & Value

• Name specifies the name of the cookie. It is a required attribute.

• Value specifies the value of the cookie. It is an option attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


33
MHSSP
4.5 Cookies
• Attributes of cookies: Secure
• It specifies whether or not the cookie should only be transmitted
over a secure HTTPS connection.

• TRUE indicates that the cookie will only be set if a secure


connection exists.

• Default is FALSE.

• It is an optional attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


34
MHSSP
4.5 Cookies
• Attributes of cookies: Domain
• It specifies the domain for which the cookie is valid and can be
submitted with every request for this domain or its sub domain.

• If this attribute is not specified, then the host name of the


originating server is used as the default value.

• It is an optional attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


35
MHSSP
4.5 Cookies
• Attributes of cookies: Domain
• E.g. if a cookie is set by an application at app.mydomain.com with no
domain attribute set, then the cookie will be resubmitted for all
the subsequent requests for app.mydomain.com & its subdomain but
not to otherapp.mydomain.com

• If domain attribute is set to mydomain.com, then cookie would be


sent to all requests for app.mydomain.com and mydomain.com.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


36
MHSSP
4.5 Cookies
• Attributes of cookies: Path
• signifies the URL or path for which the cookie is valid.

• If set to "/", the cookie will be available within the entire domain.
If set to "/php/", the cookie will only be available within the php
directory and all sub-directories of php.

• The default value is the current directory that the cookie is being
set in.

• It is an optional attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


37
MHSSP
4.5 Cookies
• Attributes of cookies: HTTPOnly

• If set to TRUE the cookie will be accessible only through the HTTP
protocol (the cookie will not be accessible by scripting languages).

• This setting can help to reduce identity theft through XSS (Cross-
site Scripting) attacks.

• Default is FALSE.

• This is an optional attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


38
MHSSP
4.5 Cookies
• Attributes of cookies: Expires
• Specifies when the cookie expires.

• If this parameter is omitted or set to 0, the cookie will expire at


the end of the session (when the browser closes).

• Default is 0.

• It is an optional attribute.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


39
MHSSP
4.5 Cookies
• Creating a Cookie:

• A cookie can be created using setcookie( ) function.

Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);

• Only name parameter is required.

• All other parameters are optional.

• The setcookie() function must appear BEFORE the <html> tag.


Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
40
MHSSP
4.5 Cookies
• Creating a Cookie:
<?php $cookie_name = "user";
$cookie_value = "Mohammed Zaid";
setcookie($cookie_name, $cookie_value,
time() + (86400 * 30), "/"); // 86400 = 1 day ?>
<html>
<body>
<?php if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}?>
</body></html>
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
41
MHSSP
4.5 Cookies
• Modifying a Cookie:

• To modify a cookie, just set (again) the cookie using the


setcookie( ) function.

• The value of the cookie is automatically URLencoded when sending


the cookie.

• It is automatically decoded when received.

• To prevent URLencoding, use setrawcookie() instead.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


42
MHSSP
4.5 Cookies
• Deleting a Cookie:

• To delete a cookie, use the setcookie function with an expiration


date in the past.

• E.g. time( ) – 3600


set expiry date with current time – value.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


43
MHSSP
4.6 Session
• Use of Session:

• Session is a way to store information (in variables) to be used


across multiple web pages.

• Unlike a cookie, the information is not stored on the user’s


computer.

• By default session variables last until the user closes the browser.

• Session variables hold information about one single user, and are
available to all pages in one application.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
44
MHSSP
4.6 Session
• Start of Session:

• Session_start( ) function can be used to start a session.

• Session variables are set with the PHP global variable: $_SESSION

• The session_start() function must be the very first thing in your


document. Before any HTML tags.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


45
MHSSP
4.6 Session
• Start of Session:

• To remove all global session variables, use session_unset( ) and


function.

• To destroy the session, use session_destroy( ) function.

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


46
MHSSP
4.6 Session
• Start of Session:
<?php
session_start(); ?>
<html> <body>
<?php
$_SESSION["favcolor"] = "Black";
$_SESSION["favanimal"] = "Snake";
echo "Session variables are set.";
echo "<br>Favourite color: ".$_SESSION["favcolor"]."<br>Favourite animal:
".$_SESSION["favanimal"];
session_unset();
session_destroy(); ?>
</body> </html>

Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,


47
MHSSP
4.7 Sending E-mail
• mail( ):
• mail( ) function allows user to send emails directly from a script.

Syntax:
mail(to,subject,message,headers,parameters);

Where:
To : Required. Specifies the receiver/s of the email.
Subject: Required. Specifies the subject of the email. It cannot contain any
newline characters.
Message: Required. Defines the message to be sent. Each line should be
separated with (\n). Line should not exceed 70 characters.
Headers: Optional. Specifies additional headers, Cc and Bcc.
Parameters: Optional. Specifies an additional parameter to the sendmail
program.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
48
MHSSP

You might also like