Different Types of Forms in PHP
Forms are indispensable in web development. We use them all the time whenever there is a need to interact with the user. Login pages, sign up pages and comment sections all include forms. There are three parts in form building. First is HTML which is used to create the forms, then CSS is used to make their design better and finally PHP is used to get the values from them. In this tutorial, we will discuss only the creation of a form and getting the values using PHP.
Components of a Form in HTML
There are different components of a form including Text Fields, text areas, radio buttons, checkboxes, buttons and many more. We will discuss some of them here and others when needed. The complete process of creating these components in HTML is being given here. We will only stick to input tag for this tutorial but there are some other types as well.
Most of the components of a form can be created using an Input tag. We always put this tag inside of a <form> tag. We use this tag as
<input type=””></input> and inside this type=“” we can put a lot of values here for example.
As a result of it here is what we get.
Other options to put inside the type=”” are number, colour, date, email, file, hidden, image, month, password, range, reset, search, TEL and URL. Use them one by one to see what’s the purpose of each.
Now it’s time to get values from these forms. As discussed we will use PHP for that.
Using PHP to Get Values from a Form– Forms in PHP
In this example, we will create a simple form to get name and age from a user and then print it on the screen, let’s start!
Create a simple form consisting of a Name field, Age field and a submit button so that when the user presses the submit button, we show all the values in the text fields and output them. But before doing this we must specify in the form tag that where we want to all these values go. Since we want them to use in this page. We will mention the name of this page as forms.php. Also we need to specify a method between “get” and “post” to get the values. An actual form tag should look like
<form action=”forms.php” method=”post”> or <form action=”forms.php” method=”get”>
It is a method to get the values from a form as discussed. But it has a flaw that it shows all the values to in the URL. That feature makes this method really unsafe. Users will be seeing all sensitive data in their URLs. But it is handy if you have some data that you don’t want to hide from the world, so can use get method. Here is a complete form
The first line of the form indicates the things we already discussed. Noticed a name attribute inside an input tag? This is really important while getting value from this field. Another thing to notice is the type of field which is number. This type only allows the user to enter a numeric value. Here is the code to get the values of this form when clicked on submit button
I entered sample values of John and 31 and here is the output
Noticed the URL filed? This is what the problem is with the get method. Now let’s jump to a secure method.
Leave everything at its place and just change the method of the form to post now and while receiving the values change $_GET to $_POST. Here is what the code should look like
This is the output when selecting the name as Mike and age as 21. Check the URL field now. There are no more values shown there because of the Post method in PHP.
So another exciting tutorial comes to an end here. We hope you enjoyed learning that. This series of tutorials will now be directed to the start of a completely new website. We will build this website from scratch using all this knowledge and with upcoming. In the next tutorial, we will build a signup page for that website and integrate it into the MySQL database. Stay connected and let us know your suggestions through comments.
Part Three: PHP WORKING WITH DATA TYPES AND VARIABLES
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Main Image Source : Pixabay