Professional Documents
Culture Documents
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.
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.
Output:
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.
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:
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:
https://www.youtube.com/watch?v=Z9aePaXve6s
https://www.youtube.com/watch?v=4q0gYjAVonI
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
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.
Browser:
PHP Script:
Browser Output
Intext question
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
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