You are on page 1of 14

STUDY MODULE 3.

1
PHP FORMS:
Introduction
You can store, update, or even delete data from the database with the help of dynamic
website/webpage forms. These forms are provided with some empty fields which provides a
means of getting user data/information. forms are created using HTML tags, when forms are
filled by the user and submitted PHP acts as the transporter of these collected data/user
information from the front end (user interface) in an organized manner and Posting it to its
expected destination which is usually a server for any further processing and then to a predefined
database storage system, so it can later be retrieved, manipulated and presented in a way that
suites the system application that is meant for.

1.0Learning Outcome
At the end of this study session, you be able to:
i. Have a clear understanding of PHP web form
ii. Know how to create HTML form
iii. Understand form HTTP Method

2.0 FORMS
What Are Forms?
In HTML, we can create forms with inputs like text fields, text areas, checkboxes, radio
buttons, etc. as a medium to getting data/user information from a user.
In web development, handling a HTML form is perhaps the most important process.
Two steps are involved:
 Creating the HTML form
 Creating the corresponding PHP script that will receive and process the form data

Uses of Forms
Almost every website has forms of some kind depending on what it is designed for. Some of
the uses of forms for data collection including:

 Comment forms
 Subscribe forms
 Contact us forms
 Search Forms
 Forms are really handy when creating dynamic websites.
 User data information collection
2.1 HTML FORMS
To understand How HTML Form Works, Lets create a simple HTML form and analyze how
it works and also understand the attributes available in the <form> tag and what are they used
for.

The <form></form> tag is use for creation of HTML forms. The below code snippet will
demonstrate its working.

Code sample using the POST Method

Output:

In the code above, we have used the <form> tag to create an HTML form, with input fields for
Name and Email along with submit button to submit the form-data.

In the <form> tag, we have two attributes, action and method, do you know what they are for?

 action: Using this attribute, we can specify the name of the file which will collect and
handle the form-data. In the example above, we have provided name of a Php file.
 method: This attribute specify the means of sending the form-data, whether it will be
submitted via POST method or GET method.
In the above code sample we used the POST as the sending attribute, we will use the GET
method in the below code sample. I.e. either can be used.

Code sample using the GET Method

Output:

2.2 FORM HTTP METHOD

PHP provides two superglobals $_GET and $_POST for collecting form-data for processing.
HTTP is an internet protocol, a set of rules, which makes data communication possible over the
internet. This protocol defines some methods to indicate the action to be performed on the server.
There are two main HTTP methods that we often use to create PHP forms as you will see in the
code example below, they are:
i. GET
ii. POST
When you open a link in your browser, the browser uses the GET method by default. In both
methods, data can be sent as key-value pairs.

The form method is case-insensitive. It means that you can use either post or POST. If you don’t
specify the method attribute, the form element will use the get method by default.
Typically, a form has one or more input elements including text, password, checkbox, radio
button, select, file upload, etc. The input elements are often called form fields.
An input element has the following important attributes name, id, type, and value. The name
attribute will be used for accessing the value in PHP.

DIAGRAM SHOWING HOW FORM DATA IS PASSED FROM THE USER


INTERFACE TO THE SERVER FOR PROCESSING

2.3 HTTP POST method

If a form uses the POST method, the web browser will include the form data in the HTTP
request’s body. After submitting the form, you can access the form data via the associative array
$_POST in PHP.

For example, if a form has an input element with the name email, you can access the email value
in PHP via the $_POST['email']. If the form doesn’t have an email input, the $_POST won’t
have any element with the key 'email'.

To check if the form data contains the email, you use the isset() like this:

2.3 HTTP GET method

When you submit a form using the GET method, you can access the form data in PHP via the
associative array $_GET.

Unlike the POST method, the GET method appends the form data in the URL that processes the
form. Suppose the URL that processes the form is http://localhost/form.php. When you enter the
email as hello@phptutorial.net and submit a form, you’ll see that the email value is appended to
the URL like this:

Intext question and

Question: what are web forms?


Forms are used for data/user information collection.
Question2: what is the tag use for form creation?
<form></form>
Question3: list two important attribute that is found in the opening form tag<form >?
 Action
 Method
Question4: differentiate between the GET AND POST method with an example
 POST: data is sent in the body of the HTTP request
 GET: data is sent in the URL

3.0Summary and Conclusion


When we develop a website or a web application, we often have to create forms to take input
from users, like a Login form or a Registration form. Creating a form on the webpage is
accomplished using HTML, while PHP serves as a transport for those values from the
webpage to the server and then in further processing those values.

4.0 Self-Assessment Question


 Learn about the common attributes that are used in with forms
 Expand your knowledge of other form input elements and types

5.0 Additional Activities (Videos, Animations & Out of Class activities)

https://www.youtube.com/watch?v=Z9aePaXve6s
https://www.youtube.com/watch?v=4q0gYjAVonI

6.0 Reference/ Further Reading


 HTML JavaScript PHP And MySQL Pdf PDF Book
 PHP, MySQL, JavaScript & HTML5 All-In-One For Dummies
 Learning PHP, MySQL, JavaScript, CSS & HTML5: A Step-by-Step Guide to
Creating Dynamic Website.
Study Session 3.2
Work with form data & how to get data from text boxes, password fields, hidden fields and
text area

Introduction
In this lecture you'll learn how to collect user inputs submitted through a form using the PHP
superglobals variables $_GET, $_POST and $_REQUEST

1.0Learning Outcome
At the end of this study session, you be able to:
i. Understand how PHP works with text-fields and text areas
ii. Understand how PHP works with check and radio boxes
iii. Understand how PHP works with different input types

2.0 Creating a Simple Contact Form


In this lecture we are going to create a simple HMTL contact form that allows users to enter
their comment and feedback into a form then displays it to the browser using PHP. Hence
demonstrating how PHP interact with HTML in extracting form data on the server side.

To access the value of a particular form field, you can use the following super-global
variables. These variables are available in all scopes throughout a script.

2.1 Capturing form data from text-field and text-area


We will create a simple form that takes a user name and display a welcome message
using PHP.
The Form
Browser:

PHP scripts to extract form data:

Welcome from PHP:


2.2 Capturing form data from check and radio boxes
We will create a simple form that takes a user gender and course selection using radio
and check boxes and display it using PHP.
The Form:

Browser:

PHP Script to get Data:


PHP Response:

2.2 Capturing form data from password field


We will create a simple form that takes a username and password using password input type
and display a welcome message using PHP.
Browser:

PHP Script:
Browser Output

Intext question

Question: differentiate between the $superglobal variable: $_GET, $_POST, $_REQUEST


 $_GET : Data encoded in the URL
 $_POST : Data encoded in the body of request
 $_REQUEST: Contains the values of both the $_GET and $_POST variables as well
as the values of the $_COOKIE superglobal variable.
Question: differentiate between the input text and input password type?

 Input type =”text” := text is vidible here


 Input type =”password” := text is encrypted here

Question: explain how to extract data from the checkbox array called courses []?

 To extract values from the course array a loop can be used to iterate over the
array

3.0Summary and Conclusion


PHP provides concise and consistent methods of working with forms and form data, these
methods can be one of the following $superglobal variable i.e $_POST, $_GET and
$_REQUEST.
While working with form data it is important to always check if field data are set before using it
to avoid errors. The isset() and isempty() functions can be used for this validation. And variables
can be declared to collect the values entered by the user for further processing of data.

4.0 Self-Assessment Question


 Student should practice using other forms of request method that were not touched in this
lecture. i.e. THE $_REQUEST[]
 Student should explore other form elements that were not looked into for board
knowledge

5.0 Additional Activities (Videos, Animations & Out of Class activities)

https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation
https://www.youtube.com/watch?v=xqI2hdDn47k
https://www.youtube.com/watch?v=g7x4JO0YW1s

6.0 Reference/ Further Reading


 HTML JavaScript PHP And MySQL Pdf PDF Book
 PHP, MySQL, JavaScript & HTML5 All-In-One For Dummies
 Learning PHP, MySQL, JavaScript, CSS & HTML5: A Step-by-Step Guide to
Creating Dynamic Website.

You might also like