SlideShare una empresa de Scribd logo
1 de 27
Handling User Input
using PHP
2PHP Programming with MySQL, 2nd Edition
Objectives
Learn about:
-Autoglobal variables
-Building Web forms
-Processing form data
-Handling submitted form data
-Creating an All-in-One forms
-Displaying 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
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.
$_SERVER["SCRIPT_NAME"];//displays the path
and name of the current script
5PHP Programming with MySQL, 2nd Edition
Web forms are interactive controls that
allow users to enter and submit data to a
processing script
A Web form requires 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
GET vs POST
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
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
The method Attribute
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
The method Attribute
The form data is separated from the URL by a question
mark (?)
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
The method Attribute
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
http://www.example.net/process_Scholarship.
php?fName=John&lName=Smith&Submit=Send+Form
12PHP Programming with MySQL, 2nd Edition
Get method Pros/Cons
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 (for debugging) 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
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
RetrievingThe PHP script that processes the user-
submitted data is called a form handler.
Submitted Data
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
16PHP Programming with MySQL, 2nd Edition
Handling Special Characters
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
Checking Form Variables for 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
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
26PHP 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
27PHP 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
}

Más contenido relacionado

La actualidad más candente

Oracle Form material
Oracle Form materialOracle Form material
Oracle Form materialRajesh Ch
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on lineMilind Patil
 
Data weave documentation
Data weave documentationData weave documentation
Data weave documentationSindhu VL
 
Passing table to subroutine
Passing table to subroutinePassing table to subroutine
Passing table to subroutineRajee Chandru
 
Sap abap interview questions
Sap abap interview questionsSap abap interview questions
Sap abap interview questionskssr99
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)Ehtisham Ali
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
Sap abap tutorial 1 (1)
Sap abap tutorial 1 (1)Sap abap tutorial 1 (1)
Sap abap tutorial 1 (1)Harshul Phadke
 
Sap abap modularization interview questions
Sap abap modularization interview questionsSap abap modularization interview questions
Sap abap modularization interview questionsPradipta Mohanty
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques newJeet Thombare
 
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookMuralidharan Radhakrishnan
 
Data weave component
Data weave componentData weave component
Data weave componentSindhu VL
 
Abap course chapter 7 abap objects and bsp
Abap course   chapter 7 abap objects and bspAbap course   chapter 7 abap objects and bsp
Abap course chapter 7 abap objects and bspMilind Patil
 
XLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & TricksXLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & Tricksguest92a5de
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)RajKumarSingh213
 
Sql intro
Sql introSql intro
Sql introglubox
 

La actualidad más candente (20)

d2k
d2kd2k
d2k
 
Oracle Form material
Oracle Form materialOracle Form material
Oracle Form material
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on line
 
Data weave documentation
Data weave documentationData weave documentation
Data weave documentation
 
Passing table to subroutine
Passing table to subroutinePassing table to subroutine
Passing table to subroutine
 
Sap abap interview questions
Sap abap interview questionsSap abap interview questions
Sap abap interview questions
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 
D2 k word_format
D2 k word_formatD2 k word_format
D2 k word_format
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
Sap abap tutorial 1 (1)
Sap abap tutorial 1 (1)Sap abap tutorial 1 (1)
Sap abap tutorial 1 (1)
 
oracle-reports6i
oracle-reports6ioracle-reports6i
oracle-reports6i
 
Sap abap modularization interview questions
Sap abap modularization interview questionsSap abap modularization interview questions
Sap abap modularization interview questions
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques new
 
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
 
Data weave component
Data weave componentData weave component
Data weave component
 
Abap course chapter 7 abap objects and bsp
Abap course   chapter 7 abap objects and bspAbap course   chapter 7 abap objects and bsp
Abap course chapter 7 abap objects and bsp
 
XLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & TricksXLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & Tricks
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)
 
Sql intro
Sql introSql intro
Sql intro
 
OpenRefine
OpenRefineOpenRefine
OpenRefine
 

Destacado

Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Sabu Francis
 
Introduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in LogicIntroduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in LogicVishal Tandel
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : BasicsMitul Desai
 

Destacado (7)

Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1
 
Introduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in LogicIntroduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in Logic
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
 
Introduction to Prolog
Introduction to PrologIntroduction to Prolog
Introduction to Prolog
 
Prolog basics
Prolog basicsProlog basics
Prolog basics
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 

Similar a Handling User Input and Processing Form Data

Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsManuel Lemos
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Developmentw3ondemand
 
UI5Con presentation on UI5 OData V4 Model
UI5Con presentation on UI5 OData V4 ModelUI5Con presentation on UI5 OData V4 Model
UI5Con presentation on UI5 OData V4 ModelPatric Ksinsik
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erickokelloerick
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26SynapseindiaComplaints
 
Programming with php
Programming with phpProgramming with php
Programming with phpsalissal
 
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
 
Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Nicole Ryan
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!Evan Mullins
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API trainingMurylo Batista
 
Laravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleLaravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleKaty Slemon
 
Why You Should Use TAPIs
Why You Should Use TAPIsWhy You Should Use TAPIs
Why You Should Use TAPIsJeffrey Kemp
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4Mudasir Syed
 

Similar a Handling User Input and Processing Form Data (20)

forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Forms With Ajax And Advanced Plugins
Forms With Ajax And Advanced PluginsForms With Ajax And Advanced Plugins
Forms With Ajax And Advanced Plugins
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Development
 
Php forms
Php formsPhp forms
Php forms
 
UI5Con presentation on UI5 OData V4 Model
UI5Con presentation on UI5 OData V4 ModelUI5Con presentation on UI5 OData V4 Model
UI5Con presentation on UI5 OData V4 Model
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
Web I - 04 - Forms
Web I - 04 - FormsWeb I - 04 - Forms
Web I - 04 - Forms
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
 
Programming with php
Programming with phpProgramming with php
Programming with php
 
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
 
Web Server Programming - Chapter 1
Web Server Programming - Chapter 1Web Server Programming - Chapter 1
Web Server Programming - Chapter 1
 
Sending data in cgi
Sending data in cgiSending data in cgi
Sending data in cgi
 
Unit 2 web technologies
Unit 2 web technologiesUnit 2 web technologies
Unit 2 web technologies
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API training
 
Laravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleLaravel 8 export data as excel file with example
Laravel 8 export data as excel file with example
 
Php 26
Php 26Php 26
Php 26
 
Why You Should Use TAPIs
Why You Should Use TAPIsWhy You Should Use TAPIs
Why You Should Use TAPIs
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
 

Más de Nicole Ryan

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving PerformanceNicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search enginesNicole Ryan
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and AudioNicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with ImagesNicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and SetsNicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and AnimationNicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web FormsNicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and TablesNicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your websiteNicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with LinksNicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSSNicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSSNicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSSNicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web ContentNicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your WebsiteNicole Ryan
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationNicole Ryan
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 

Más de Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
 
Inheritance
InheritanceInheritance
Inheritance
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
 
Working with Images
Working with ImagesWorking with Images
Working with Images
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
 
Working with Links
Working with LinksWorking with Links
Working with Links
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 

Último

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Último (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Handling User Input and Processing Form Data

  • 2. 2PHP Programming with MySQL, 2nd Edition Objectives Learn about: -Autoglobal variables -Building Web forms -Processing form data -Handling submitted form data -Creating an All-in-One forms -Displaying 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 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. $_SERVER["SCRIPT_NAME"];//displays the path and name of the current script
  • 5. 5PHP Programming with MySQL, 2nd Edition Web forms are interactive controls that allow users to enter and submit data to a processing script A Web form requires 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 GET vs POST 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 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 The method Attribute 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 The method Attribute The form data is separated from the URL by a question mark (?) 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 The method Attribute 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 http://www.example.net/process_Scholarship. php?fName=John&lName=Smith&Submit=Send+Form
  • 12. 12PHP Programming with MySQL, 2nd Edition Get method Pros/Cons 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 (for debugging) 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 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 RetrievingThe PHP script that processes the user- submitted data is called a form handler. Submitted Data 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 16PHP Programming with MySQL, 2nd Edition
  • 17. Handling Special Characters 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 Checking Form Variables for 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 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
  • 26. 26PHP 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
  • 27. 27PHP 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 }