Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Introduction to PHP Forms

 When you are login into your mail box with your username and password, you
are interacting with a form.
 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.

1
What is a Form
 Forms are used as a medium for gathering and collection of user
data/information
 A form contains graphical user interface items such as text input box, radio
buttons, check buttons etc.
 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.

2
Uses of PHP 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

3
PHP Form Action and Method Attributes

 There are two important attributes within the opening <form> tag: namely:
• Action
• Method
<Form action = “ ” method = “” ></form>
 The action attribute references a PHP file that receives the data entered into
the form when user submit it by pressing the submit button.
 The method attribute tells the browser to send the form data through
$_GET[] or $_POST[] method.
 The rest of the elements inside the form are basic form controls to receive
user inputs.

4
PHP 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:
 $_GET[]
 $_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.

5
DIAGRAM SHOWING HOW FORM DATA IS PASSED FROM THE
USER INTERFACE TO THE SERVER FOR PROCESSING

6
Form 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:

7
Form 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

8
Differences between GET and POST

9
Creating a form
Using HTML tags to we can create different types forms, Below is the minimal list
of things you need to create a form.

 Opening and closing form tags <form>…</form>


 Form submission type POST or GET
 Submission URL that will process the submitted data
 Input fields such as input boxes, text areas, buttons, checkboxes etc.

10
Simple Registration form example

11
Analyzing the form
 <form…>…</form> are the opening and closing form tags
 action=”registration_form.php” method=”POST”> specifies the destination URL and
the submission type.
 First/Last name: are labels for the input boxes
 <input type=”text”…> are input box tags
 <input type=”hidden” name=”form_submitted” value=”1″/> is a hidden value that is
used to check whether the form has been submitted or not
 <input type=”submit” value=”Submit”> is the button that when clicked submits the
form to the server for processing

12
Submitting the form data to the server
 The action attribute of the form specifies the submission URL that processes the
data. The method attribute specifies the submission type.

13
Capturing Form Data with PHP

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

14
Capturing Form Data Cont’d
Taking the contact form on slide11 & 13 as example with the following attribute:
 Action = “registration_form.php” // specifies where to send the form
 Method = “POST” // specifies the method used
Note: on the registration_form.php where the form is submitted and processed,
we can get the form data in the following way:
$first_name = $_POST[‘firstname’];
$last_name = $_POST[‘lastname’];
This will save the fromdata firstname and lastname into a variable while the
code below will go ahead to print it
echo $_POST[‘firstname’];
echo $_POST[‘lastname’]

15
Processing the form data cont’d
 The registration form submits data to location as specified in the action
attribute of the form.
 When a form has been submitted, the values are populated in the either the
$_GET or $_POST super global array depending on what is used.
 We will use the PHP isset() function to check if the form values have been
filled in the $_GET or $_POST array.
 To get the data passed in $_GET or $_POST method we use
$_GET[‘variable_name’] or $name = $_POST[‘variable_name’];

16
Getting the Form Data with PHP

17
Other form Control Elements
 Other basic form control elements includes:
 Text Input Controls
 Checkboxes Controls
 Radio Box Controls
 Select Box Controls
 File Select boxes
 Hidden Controls

 NOTE: we will see how to get the form data of the above form element in the
practical session.

18
The end

19

You might also like