SlideShare a Scribd company logo
1 of 44
Chapter 4
Handling User Input
PHP Programming with MySQL
2nd
Edition
2PHP Programming with MySQL, 2nd Edition
Objectives
In this chapter, you will:
• Learn about autoglobal variables
• Build XHTML Web forms
• Process form data
• Handle submitted form data
• Create an All-in-One form
• Display dynamic data based on a URL token
3PHP Programming with MySQL, 2nd Edition
Using Autoglobals
• Autoglobals are predefined global arrays that
provide information about server, environment,
and user input
4PHP Programming with MySQL, 2nd Edition
Using Autoglobals (continued)
• Autoglobals are associative arrays
– To access the values in an associative array,
place the element’s key in single or double
quotation marks inside the array brackets.
(the following example displays the
SCRIPT_NAME element of the $_SERVER
autoglobal)
$_SERVER["SCRIPT_NAME"];//displays
the path and name of the current
script
5PHP Programming with MySQL, 2nd Edition
Building XHTML Web Forms
• Web forms are interactive controls that allow
users to enter and submit data to a processing
script
• A Web form is a standard XHTML form with two
required attributes in the opening <form> tag:
– Action attribute: Identifies the program on the
Web server that will process the form data when
it is submitted
– Method attribute: Specifies how the form data
will be sent to the processing script
6PHP Programming with MySQL, 2nd Edition
Adding an action Attribute
• The opening form tag requires an action
attribute
• The value of the action attribute identifies the
program on the Web server that will process the
form data when the form is submitted
<form action="http://www.example.com/
HandleFormInput.php">
7PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
• The value of the method attribute must be either
“post” or “get”
– The “post” method embeds the form data in the
request message
– The “get” method appends the form data to the
URL specified in the form’s action attribute
• When a Web form is submitted using the “post”
method, PHP automatically creates and
populates a $_POST array; when the “get”
method is used, PHP creates and populates a
$_GET array
8PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
(continued)
• Form fields are sent to the Web server as a
name/value pair
– The name portion of the name/value pair
becomes the key of an element in the $_POST or
$_GET array, depending on which method was
used to submit the data
– The value portion of the name/value pair is
populated by the data that the user enters in the
input control on the Web form
9PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
(continued)
• When submitting data using the “get” method,
form data is appended to the URL specified by
the action attribute
• Name/value pairs appended to the URL are
called URL tokens
10PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
(continued)
• The form data is separated from the URL by a
question mark (?)
• the individual elements are separated by an
ampersand (&)
• the element name is separated from the value
by an equal sign (=).
• Spaces in the name and value fields are
encoded as plus signs (+)
11PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
(continued)
– all other characters except letters, numbers,
hyphens (-), underscores (_) and periods (.) are
encoded using a percent sign (%) followed by the
two-digit hexadecimal representation of the
character’s ASCII value
• (the following code shows three form elements
submitted to the process_Scholarship.php script)
http://www.example.net/process_Scholars
hip.php?
fName=John&lName=Smith&Submit=Send+Form
12PHP Programming with MySQL, 2nd Edition
Adding the method Attribute
(continued)
• Limitations of the “get” method for submitting
form data
– Restricts the number of characters that can be
appended to a single variable to 100
– The form values are appended to the URL in
plain text, making a URL request insecure
• Advantage of the “get” method for submitting
form data
– Passed values are visible in the Address Bar of
the browser
13PHP Programming with MySQL, 2nd Edition
Processing Form Data
• A form handler is a program or script that
processes the information submitted from a Web
form
• A form handler performs the following:
– Verifies that the user entered the minimum
amount of data to process the form
– Validates form data
– Works with the submitted data
– Returns appropriate output as a Web page
14PHP Programming with MySQL, 2nd Edition
Retrieving Submitted Data
• The PHP script that processes the user-
submitted data is called a form handler.
• The values stored in the $_POST array can be
accessed and displayed by the echo statement
as shown below:
$firstName = $_POST['fName'];
$lastName = $_POST['lName'];
echo "Thank you for filling out the
scholarship form, ".$firstName." ".
$lastName . ".";
15PHP Programming with MySQL, 2nd Edition
Handling Special Characters
• Magic Quotes automatically add a backslash
character to any single quote, double quote, or
NULL character contained in form data that a
user submits to a PHP script
Figure 4-4 Form input string with magic quotes
Handling Special Characters
(continued)
16PHP Programming with MySQL, 2nd Edition
Handling Special Characters
(continued)
• The addslashes() function adds a backslash
before a single or double quote or a NULL
character in user input (if magic quotes is
disabled, this is the alternative to escape a
character before saving to a text file or database)
• The stripslashes() function removes a
backslash before a single or double quote or
NULL character in user input (if magic quotes is
enabled, this is required before outputting a string
with the echo statement)
17PHP Programming with MySQL, 2nd Edition
18PHP Programming with MySQL, 2nd Edition
Handling Submitted Form Data
• It is necessary to validate Web form data to
ensure PHP can use the data
• The optimal way to ensure valid form data is
only allow the user to enter an acceptable
response
• Examples of data validation include verifying that
– the user did not leave any required fields blank
– an e-mail address was entered in the correct
format
– the user did not exceed the word limit in a
comment box
19PHP Programming with MySQL, 2nd Edition
Determining if Form Variables
Contain Values
• When form data is posted using the “post” or
“get” method, all controls except unchecked
radio buttons and checkboxes get sent to the
server even if they do not contain data
• The empty() function is used to determine if a
variable contains a value
• The empty() function returns FALSE if the
variable being checked has a nonempty and
nonzero value, and a value of TRUE if the
variable has an empty or zero value
20PHP Programming with MySQL, 2nd Edition
Validating Entered Data
• Validating form data refers to verifying that the
value entered in a field is appropriate for the
data type that should have been entered
• The best way to ensure valid form data is to
build the Web form with controls (such as check
boxes, radio buttons, and selection lists) that
only allow the user to select valid responses
• Unique information, such as user name,
password, or e-mail must be validated
21PHP Programming with MySQL, 2nd Edition
Validating Numeric Data
• All data in a Web form is string data and PHP
automatically converts string data to numeric
data if the string is a number
– The is_numeric() function is used to
determine if a variable contains a number
– The round() function can be used to a numeric
variable with an appropriate number of decimal
places
22PHP Programming with MySQL, 2nd Edition
Validating String Data
• Regular expression functions are some of the
best tools for verifying that string data meets the
strict formatting required for e-mail addresses,
Web page URLs, or date values
– The stripslashes() function removes the
leading slashes for escape sequences
– The trim() function removes any leading or
trailing white space from a string
23PHP Programming with MySQL, 2nd Edition
Handling Multiple Errors
• When processing a Web form, it is best to track
any errors on the form during processing and
then redisplay the form for the user to correct all
the errors at one time
24PHP Programming with MySQL, 2nd Edition
Redisplaying the Web Form
• A sticky form is used to redisplay the form with
the controls set to the values the user entered
the last time the form was submitted
• The following syntax illustrates how to use the
value attribute to display previous submitted
values in sticky form:
<p>First Name: <input type="text"
name="fName" value="<?php echo $firstName;
?>" /></p>
25PHP Programming with MySQL, 2nd Edition
Emailing the Web Form
• The mail() function is used to send an e-mail
message containing form data in PHP
• The basic syntax for this function is
mail(recipient(s), subject, message)
• The Address Specifier defines the format of the
e-mail addresses that can be entered as the
recipient argument
– Plain e-mail address: jdoe@example.net
– Recipients name and e-mail address: Mary
Smith <mary.smith@example.com>
26PHP Programming with MySQL, 2nd Edition
Emailing the Web Form
(continued)
• The subject argument of the mail() function
must include only plain text with no XHTML tags
or character entities unless a special MIME
format is used
• The message argument of the mail() function
is a text string that must also be in plain text
• A fourth, optional additional_headers
argument can include headers that are standard
in most e-mail editors – From, Cc, Bcc and Date.
27PHP Programming with MySQL, 2nd Edition
Emailing the Web Form
(continued)
With the additional_headers argument
– Each header must be on its own line
– Each line must start with the header name,
followed by a colon, a space, and the value of the
header element
Date: Fri, 03 Apr 2009 16:05:50 -0400
From: Linda M. Jones
linda@jones.example.com
CC: Mary R. Jones <mary@jones.example.com>
• A successful e-mail message returns a value of
TRUE
28PHP Programming with MySQL, 2nd Edition
Creating an All-in-One Form
• A two-part form has one page that displays the
form and one page that processes the form data
• For simple forms that require only minimal
processing, it’s often easier to use an All-in-One
form—a single script used display a Web form
and process its data
29PHP Programming with MySQL, 2nd Edition
Validating an All-in-One Form
• It uses a conditional to determine if the form has
been submitted or if it is being viewed for the first
time
– The isset() function is used to determine if the
$Submit variable has been set
if (isset($Submit)) {
// Validate the data
}
– The argument of the isset() function is the name
assigned to the Submit button in the Web form
30PHP Programming with MySQL, 2nd Edition
Redisplaying the Web Form
• If the submitted data did not pass all validation
checks or no data has been entered, the All-in-
One form will display the Web form, for the user
to enter data for the first time or re-enter data
that did not pass validation
if (isset ($_POST['Submit'])) {
// Process the data
}
else {
// Display the Web form
}
31PHP Programming with MySQL, 2nd Edition
Displaying Dynamic Content Based
on a URL Token
• By passing URL tokens to a PHP script, many
different types of information can be displayed
from the same script
• By using a Web page template with static
sections and a dynamic content section, a single
PHP script can produce the same content as
multiple static XHTML pages
32PHP Programming with MySQL, 2nd Edition
Using a Web Page Template
• A Web template is a single Web page that is
divided into separate sections such as
– Header
– Button Navigation
– Dynamic Content
– Footer
• The contents of the individual sections are
populated using include files
33PHP Programming with MySQL, 2nd Edition
Using Text Hyperlinks for Navigation
• When the user clicks on a text hyperlink the
contents that display in the dynamic data section
of the index.htm (home page) are replaced by
the contents referenced by the href attribute
• A name/value pair is appended to the index URL
(this attribute and value will be referenced in the
dynamic data section of the index.php file)
– The name is user defined
– The value is user defined
<a href = "index.php?page=home_page">Home</a>
34PHP Programming with MySQL, 2nd Edition
Using Form Image Buttons
for Navigation
• Buttons must be enclosed by a opening and
closing <form> tag
<input type = "image" src = "home.jpg" name =
"home" style = "border:0" alt= "Home" />
• x- and y- coordinates are sent in the form
“Button.x” and “Button.y” where “Button” is the
value of the name attribute (home)
• In PHP, the periods are replaced by underscores
for the $_GET or $_POST array indexes
• The $_GET and $_POST array would have two
elements “home_x” and “home_y”
35PHP Programming with MySQL, 2nd Edition
Displaying the Dynamic Content
• The $_REQUEST autoglobal can be used to
access the results from form data sent using
either the “get” or “post” methods
– The syntax to save the value of the page attribute
to a variable is shown below:
$displayContents = $_REQUEST["page"];
• The dynamic content section of the index.php file
will contain the code to determine which content
page to display
36PHP Programming with MySQL, 2nd Edition
Displaying the Dynamic Content
(continued)
if (isset($_GET['page'])) {
switch ($_GET['page']) {
case 'About Me':
include('inc_about.html');
break;
case 'home'://display the default page
include('inc_home.html');
break;
default:
include('inc_home.html');
break;
}
}
37PHP Programming with MySQL, 2nd Edition
Summary
• PHP includes various predefined global arrays,
called autoglobals or superglobals, which
contain client, server, and environment
information that you can use in your scripts
• Web forms are standard XHTML Web pages
with interactive controls that allow users to enter
data
38PHP Programming with MySQL, 2nd Edition
Summary (continued)
• The <form> tag requires an action attribute to
identify the script that will process the submitted
data and a method attribute to identify whether
the data will be sent using the “get” or “post”
method
• The $_POST autoglobal contains data
submitted from a form using the “post” method;
the $_GET autoglobal contains data submitted
from a form using the “get” method or through a
hyperlink
39PHP Programming with MySQL, 2nd Edition
Summary (continued)
• Web forms may have two components: the data
entry form page and the data processing script
• If Magic Quotes is enabled, the PHP scripting
engine inserts an escape character before a
single quotation mark, double quotation mark, or
NULL character in any submitted form data
• Magic quotes may be enabled for a PHP server
40PHP Programming with MySQL, 2nd Edition
Summary (continued)
• The addslashes() function inserts an escape
character before a single quotation mark, double
quotation mark, or NULL character in a string
• The stripslashes() function removes the
escape character before a single quotation
mark, double quotation mark, or NULL character
in a string
• The first step in processing form data is to
validate the input
41PHP Programming with MySQL, 2nd Edition
Summary (continued)
• The empty()function determines if the entered
value has an empty or zero value
• The is_*() family of functions determines if
the entered value is of the required data type
• Regular expressions determine if an entered
string value is formatted correctly for the
required type of entry
• The user should be notified of all errors in the
values entered into the form
42PHP Programming with MySQL, 2nd Edition
Summary (continued)
• Sticky forms are forms that redisplay after an
error has been found
• The fields in a sticky form are populated with the
values the user entered previously.
• Advanced escaping from XHTML is a
convenient way to display XHTML code within a
PHP code block
43PHP Programming with MySQL, 2nd Edition
Summary (continued)
• The mail() function is used to send mail from
PHP; it can be used to send form data via e-mail
when the form has been successfully completed
and validated
• All-in-One Web forms combine the data entry
form page and the data processing script into a
single script
• The isset() function determines if the
entered value has been initialized (or set)
44PHP Programming with MySQL, 2nd Edition
Summary (continued)
• URL tokens use the “get” method and additional
data appended to the URL to submit information
to a PHP script
• Web templates combine static elements and a
dynamic content section within a Web page
• Web templates can use the include()
function within a conditional or switch statement
to display dynamic content from different include
files within the same section of the template

More Related Content

What's hot

Session 19 - MapReduce
Session 19  - MapReduce Session 19  - MapReduce
Session 19 - MapReduce AnandMHadoop
 
Developing web applications
Developing web applicationsDeveloping web applications
Developing web applicationssalissal
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow TransformationsPramod Singla
 
Session 04 pig - slides
Session 04   pig - slidesSession 04   pig - slides
Session 04 pig - slidesAnandMHadoop
 
Db2 Important questions to read
Db2 Important questions to readDb2 Important questions to read
Db2 Important questions to readPrasanth Dusi
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsZohar Elkayam
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL ISankhya_Analytics
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databasesFabio Fumarola
 
R - Get Started I - Sanaitics
R - Get Started I - SanaiticsR - Get Started I - Sanaitics
R - Get Started I - SanaiticsVijith Nair
 
Architecture of Native XML Database Sedna
Architecture of Native XML Database SednaArchitecture of Native XML Database Sedna
Architecture of Native XML Database Sednamaria.grineva
 
FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012Michael Rys
 

What's hot (19)

Session 19 - MapReduce
Session 19  - MapReduce Session 19  - MapReduce
Session 19 - MapReduce
 
Developing web applications
Developing web applicationsDeveloping web applications
Developing web applications
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
 
Session 04 pig - slides
Session 04   pig - slidesSession 04   pig - slides
Session 04 pig - slides
 
Db2 Important questions to read
Db2 Important questions to readDb2 Important questions to read
Db2 Important questions to read
 
Software Database and Testing
Software Database and TestingSoftware Database and Testing
Software Database and Testing
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databases
 
R - Get Started I - Sanaitics
R - Get Started I - SanaiticsR - Get Started I - Sanaitics
R - Get Started I - Sanaitics
 
Etl2
Etl2Etl2
Etl2
 
Architecture of Native XML Database Sedna
Architecture of Native XML Database SednaArchitecture of Native XML Database Sedna
Architecture of Native XML Database Sedna
 
Rajasekar_ORACLE_DBA
Rajasekar_ORACLE_DBARajasekar_ORACLE_DBA
Rajasekar_ORACLE_DBA
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
Unit 4 lecture-3
Unit 4 lecture-3Unit 4 lecture-3
Unit 4 lecture-3
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012FileTable and Semantic Search in SQL Server 2012
FileTable and Semantic Search in SQL Server 2012
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 

Similar to 9780538745840 ppt ch04

Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Mohd Harris Ahmad Jaal
 
Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Nicole Ryan
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API trainingMurylo Batista
 
Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsManuel Lemos
 
WEB-MODULE 4.pdf
WEB-MODULE 4.pdfWEB-MODULE 4.pdf
WEB-MODULE 4.pdfDeepika A B
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiNaveen Kumar Veligeti
 
9780538745840 ppt ch01 PHP
9780538745840 ppt ch01 PHP9780538745840 ppt ch01 PHP
9780538745840 ppt ch01 PHPTerry Yoast
 
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)Marco Balduzzi
 
Informatica overview
Informatica overviewInformatica overview
Informatica overviewkarthik kumar
 
Informatica overview
Informatica overviewInformatica overview
Informatica overviewkarthik kumar
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptGiyaShefin
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Developmentw3ondemand
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erickokelloerick
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxAhmedKafi7
 

Similar to 9780538745840 ppt ch04 (20)

Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
 
Php forms
Php formsPhp forms
Php forms
 
Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Web Server Programming - Chapter 1
Web Server Programming - Chapter 1
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API training
 
Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced Plugins
 
web2_lec6.pdf
web2_lec6.pdfweb2_lec6.pdf
web2_lec6.pdf
 
WEB-MODULE 4.pdf
WEB-MODULE 4.pdfWEB-MODULE 4.pdf
WEB-MODULE 4.pdf
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
9780538745840 ppt ch01 PHP
9780538745840 ppt ch01 PHP9780538745840 ppt ch01 PHP
9780538745840 ppt ch01 PHP
 
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)
HTTP Parameter Pollution Vulnerabilities in Web Applications (Black Hat EU 2011)
 
Informatica overview
Informatica overviewInformatica overview
Informatica overview
 
Informatica overview
Informatica overviewInformatica overview
Informatica overview
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Development
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptx
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

9780538745840 ppt ch04

  • 1. Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
  • 2. 2PHP Programming with MySQL, 2nd Edition Objectives In this chapter, you will: • Learn about autoglobal variables • Build XHTML Web forms • Process form data • Handle submitted form data • Create an All-in-One form • Display dynamic data based on a URL token
  • 3. 3PHP Programming with MySQL, 2nd Edition Using Autoglobals • Autoglobals are predefined global arrays that provide information about server, environment, and user input
  • 4. 4PHP Programming with MySQL, 2nd Edition Using Autoglobals (continued) • Autoglobals are associative arrays – To access the values in an associative array, place the element’s key in single or double quotation marks inside the array brackets. (the following example displays the SCRIPT_NAME element of the $_SERVER autoglobal) $_SERVER["SCRIPT_NAME"];//displays the path and name of the current script
  • 5. 5PHP Programming with MySQL, 2nd Edition Building XHTML Web Forms • Web forms are interactive controls that allow users to enter and submit data to a processing script • A Web form is a standard XHTML form with two required attributes in the opening <form> tag: – Action attribute: Identifies the program on the Web server that will process the form data when it is submitted – Method attribute: Specifies how the form data will be sent to the processing script
  • 6. 6PHP Programming with MySQL, 2nd Edition Adding an action Attribute • The opening form tag requires an action attribute • The value of the action attribute identifies the program on the Web server that will process the form data when the form is submitted <form action="http://www.example.com/ HandleFormInput.php">
  • 7. 7PHP Programming with MySQL, 2nd Edition Adding the method Attribute • The value of the method attribute must be either “post” or “get” – The “post” method embeds the form data in the request message – The “get” method appends the form data to the URL specified in the form’s action attribute • When a Web form is submitted using the “post” method, PHP automatically creates and populates a $_POST array; when the “get” method is used, PHP creates and populates a $_GET array
  • 8. 8PHP Programming with MySQL, 2nd Edition Adding the method Attribute (continued) • Form fields are sent to the Web server as a name/value pair – The name portion of the name/value pair becomes the key of an element in the $_POST or $_GET array, depending on which method was used to submit the data – The value portion of the name/value pair is populated by the data that the user enters in the input control on the Web form
  • 9. 9PHP Programming with MySQL, 2nd Edition Adding the method Attribute (continued) • When submitting data using the “get” method, form data is appended to the URL specified by the action attribute • Name/value pairs appended to the URL are called URL tokens
  • 10. 10PHP Programming with MySQL, 2nd Edition Adding the method Attribute (continued) • The form data is separated from the URL by a question mark (?) • the individual elements are separated by an ampersand (&) • the element name is separated from the value by an equal sign (=). • Spaces in the name and value fields are encoded as plus signs (+)
  • 11. 11PHP Programming with MySQL, 2nd Edition Adding the method Attribute (continued) – all other characters except letters, numbers, hyphens (-), underscores (_) and periods (.) are encoded using a percent sign (%) followed by the two-digit hexadecimal representation of the character’s ASCII value • (the following code shows three form elements submitted to the process_Scholarship.php script) http://www.example.net/process_Scholars hip.php? fName=John&lName=Smith&Submit=Send+Form
  • 12. 12PHP Programming with MySQL, 2nd Edition Adding the method Attribute (continued) • Limitations of the “get” method for submitting form data – Restricts the number of characters that can be appended to a single variable to 100 – The form values are appended to the URL in plain text, making a URL request insecure • Advantage of the “get” method for submitting form data – Passed values are visible in the Address Bar of the browser
  • 13. 13PHP Programming with MySQL, 2nd Edition Processing Form Data • A form handler is a program or script that processes the information submitted from a Web form • A form handler performs the following: – Verifies that the user entered the minimum amount of data to process the form – Validates form data – Works with the submitted data – Returns appropriate output as a Web page
  • 14. 14PHP Programming with MySQL, 2nd Edition Retrieving Submitted Data • The PHP script that processes the user- submitted data is called a form handler. • The values stored in the $_POST array can be accessed and displayed by the echo statement as shown below: $firstName = $_POST['fName']; $lastName = $_POST['lName']; echo "Thank you for filling out the scholarship form, ".$firstName." ". $lastName . ".";
  • 15. 15PHP Programming with MySQL, 2nd Edition Handling Special Characters • Magic Quotes automatically add a backslash character to any single quote, double quote, or NULL character contained in form data that a user submits to a PHP script Figure 4-4 Form input string with magic quotes
  • 16. Handling Special Characters (continued) 16PHP Programming with MySQL, 2nd Edition
  • 17. Handling Special Characters (continued) • The addslashes() function adds a backslash before a single or double quote or a NULL character in user input (if magic quotes is disabled, this is the alternative to escape a character before saving to a text file or database) • The stripslashes() function removes a backslash before a single or double quote or NULL character in user input (if magic quotes is enabled, this is required before outputting a string with the echo statement) 17PHP Programming with MySQL, 2nd Edition
  • 18. 18PHP Programming with MySQL, 2nd Edition Handling Submitted Form Data • It is necessary to validate Web form data to ensure PHP can use the data • The optimal way to ensure valid form data is only allow the user to enter an acceptable response • Examples of data validation include verifying that – the user did not leave any required fields blank – an e-mail address was entered in the correct format – the user did not exceed the word limit in a comment box
  • 19. 19PHP Programming with MySQL, 2nd Edition Determining if Form Variables Contain Values • When form data is posted using the “post” or “get” method, all controls except unchecked radio buttons and checkboxes get sent to the server even if they do not contain data • The empty() function is used to determine if a variable contains a value • The empty() function returns FALSE if the variable being checked has a nonempty and nonzero value, and a value of TRUE if the variable has an empty or zero value
  • 20. 20PHP Programming with MySQL, 2nd Edition Validating Entered Data • Validating form data refers to verifying that the value entered in a field is appropriate for the data type that should have been entered • The best way to ensure valid form data is to build the Web form with controls (such as check boxes, radio buttons, and selection lists) that only allow the user to select valid responses • Unique information, such as user name, password, or e-mail must be validated
  • 21. 21PHP Programming with MySQL, 2nd Edition Validating Numeric Data • All data in a Web form is string data and PHP automatically converts string data to numeric data if the string is a number – The is_numeric() function is used to determine if a variable contains a number – The round() function can be used to a numeric variable with an appropriate number of decimal places
  • 22. 22PHP Programming with MySQL, 2nd Edition Validating String Data • Regular expression functions are some of the best tools for verifying that string data meets the strict formatting required for e-mail addresses, Web page URLs, or date values – The stripslashes() function removes the leading slashes for escape sequences – The trim() function removes any leading or trailing white space from a string
  • 23. 23PHP Programming with MySQL, 2nd Edition Handling Multiple Errors • When processing a Web form, it is best to track any errors on the form during processing and then redisplay the form for the user to correct all the errors at one time
  • 24. 24PHP Programming with MySQL, 2nd Edition Redisplaying the Web Form • A sticky form is used to redisplay the form with the controls set to the values the user entered the last time the form was submitted • The following syntax illustrates how to use the value attribute to display previous submitted values in sticky form: <p>First Name: <input type="text" name="fName" value="<?php echo $firstName; ?>" /></p>
  • 25. 25PHP Programming with MySQL, 2nd Edition Emailing the Web Form • The mail() function is used to send an e-mail message containing form data in PHP • The basic syntax for this function is mail(recipient(s), subject, message) • The Address Specifier defines the format of the e-mail addresses that can be entered as the recipient argument – Plain e-mail address: jdoe@example.net – Recipients name and e-mail address: Mary Smith <mary.smith@example.com>
  • 26. 26PHP Programming with MySQL, 2nd Edition Emailing the Web Form (continued) • The subject argument of the mail() function must include only plain text with no XHTML tags or character entities unless a special MIME format is used • The message argument of the mail() function is a text string that must also be in plain text • A fourth, optional additional_headers argument can include headers that are standard in most e-mail editors – From, Cc, Bcc and Date.
  • 27. 27PHP Programming with MySQL, 2nd Edition Emailing the Web Form (continued) With the additional_headers argument – Each header must be on its own line – Each line must start with the header name, followed by a colon, a space, and the value of the header element Date: Fri, 03 Apr 2009 16:05:50 -0400 From: Linda M. Jones linda@jones.example.com CC: Mary R. Jones <mary@jones.example.com> • A successful e-mail message returns a value of TRUE
  • 28. 28PHP Programming with MySQL, 2nd Edition Creating an All-in-One Form • A two-part form has one page that displays the form and one page that processes the form data • For simple forms that require only minimal processing, it’s often easier to use an All-in-One form—a single script used display a Web form and process its data
  • 29. 29PHP Programming with MySQL, 2nd Edition Validating an All-in-One Form • It uses a conditional to determine if the form has been submitted or if it is being viewed for the first time – The isset() function is used to determine if the $Submit variable has been set if (isset($Submit)) { // Validate the data } – The argument of the isset() function is the name assigned to the Submit button in the Web form
  • 30. 30PHP Programming with MySQL, 2nd Edition Redisplaying the Web Form • If the submitted data did not pass all validation checks or no data has been entered, the All-in- One form will display the Web form, for the user to enter data for the first time or re-enter data that did not pass validation if (isset ($_POST['Submit'])) { // Process the data } else { // Display the Web form }
  • 31. 31PHP Programming with MySQL, 2nd Edition Displaying Dynamic Content Based on a URL Token • By passing URL tokens to a PHP script, many different types of information can be displayed from the same script • By using a Web page template with static sections and a dynamic content section, a single PHP script can produce the same content as multiple static XHTML pages
  • 32. 32PHP Programming with MySQL, 2nd Edition Using a Web Page Template • A Web template is a single Web page that is divided into separate sections such as – Header – Button Navigation – Dynamic Content – Footer • The contents of the individual sections are populated using include files
  • 33. 33PHP Programming with MySQL, 2nd Edition Using Text Hyperlinks for Navigation • When the user clicks on a text hyperlink the contents that display in the dynamic data section of the index.htm (home page) are replaced by the contents referenced by the href attribute • A name/value pair is appended to the index URL (this attribute and value will be referenced in the dynamic data section of the index.php file) – The name is user defined – The value is user defined <a href = "index.php?page=home_page">Home</a>
  • 34. 34PHP Programming with MySQL, 2nd Edition Using Form Image Buttons for Navigation • Buttons must be enclosed by a opening and closing <form> tag <input type = "image" src = "home.jpg" name = "home" style = "border:0" alt= "Home" /> • x- and y- coordinates are sent in the form “Button.x” and “Button.y” where “Button” is the value of the name attribute (home) • In PHP, the periods are replaced by underscores for the $_GET or $_POST array indexes • The $_GET and $_POST array would have two elements “home_x” and “home_y”
  • 35. 35PHP Programming with MySQL, 2nd Edition Displaying the Dynamic Content • The $_REQUEST autoglobal can be used to access the results from form data sent using either the “get” or “post” methods – The syntax to save the value of the page attribute to a variable is shown below: $displayContents = $_REQUEST["page"]; • The dynamic content section of the index.php file will contain the code to determine which content page to display
  • 36. 36PHP Programming with MySQL, 2nd Edition Displaying the Dynamic Content (continued) if (isset($_GET['page'])) { switch ($_GET['page']) { case 'About Me': include('inc_about.html'); break; case 'home'://display the default page include('inc_home.html'); break; default: include('inc_home.html'); break; } }
  • 37. 37PHP Programming with MySQL, 2nd Edition Summary • PHP includes various predefined global arrays, called autoglobals or superglobals, which contain client, server, and environment information that you can use in your scripts • Web forms are standard XHTML Web pages with interactive controls that allow users to enter data
  • 38. 38PHP Programming with MySQL, 2nd Edition Summary (continued) • The <form> tag requires an action attribute to identify the script that will process the submitted data and a method attribute to identify whether the data will be sent using the “get” or “post” method • The $_POST autoglobal contains data submitted from a form using the “post” method; the $_GET autoglobal contains data submitted from a form using the “get” method or through a hyperlink
  • 39. 39PHP Programming with MySQL, 2nd Edition Summary (continued) • Web forms may have two components: the data entry form page and the data processing script • If Magic Quotes is enabled, the PHP scripting engine inserts an escape character before a single quotation mark, double quotation mark, or NULL character in any submitted form data • Magic quotes may be enabled for a PHP server
  • 40. 40PHP Programming with MySQL, 2nd Edition Summary (continued) • The addslashes() function inserts an escape character before a single quotation mark, double quotation mark, or NULL character in a string • The stripslashes() function removes the escape character before a single quotation mark, double quotation mark, or NULL character in a string • The first step in processing form data is to validate the input
  • 41. 41PHP Programming with MySQL, 2nd Edition Summary (continued) • The empty()function determines if the entered value has an empty or zero value • The is_*() family of functions determines if the entered value is of the required data type • Regular expressions determine if an entered string value is formatted correctly for the required type of entry • The user should be notified of all errors in the values entered into the form
  • 42. 42PHP Programming with MySQL, 2nd Edition Summary (continued) • Sticky forms are forms that redisplay after an error has been found • The fields in a sticky form are populated with the values the user entered previously. • Advanced escaping from XHTML is a convenient way to display XHTML code within a PHP code block
  • 43. 43PHP Programming with MySQL, 2nd Edition Summary (continued) • The mail() function is used to send mail from PHP; it can be used to send form data via e-mail when the form has been successfully completed and validated • All-in-One Web forms combine the data entry form page and the data processing script into a single script • The isset() function determines if the entered value has been initialized (or set)
  • 44. 44PHP Programming with MySQL, 2nd Edition Summary (continued) • URL tokens use the “get” method and additional data appended to the URL to submit information to a PHP script • Web templates combine static elements and a dynamic content section within a Web page • Web templates can use the include() function within a conditional or switch statement to display dynamic content from different include files within the same section of the template