Forms in PHP

Forms in PHP – php form example with database

Forms are indispensable in web development. We use them all the time whenever there is a need to interact with 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 creation of a form and getting the values using PHP.

Components of a Form in HTML– Forms in PHP

There are different components of a form including Text Fields, text areas, radio buttons, check boxes, buttons and many more. We will discuss some of them here and others when needed. 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.

Input Tag

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.

Forms in PHP - PHP Form example with DatabaseAs a result of it here is what we get.

Forms in PHP - PHP Form example with DatabaseOther options to put inside the type=”” are number, color, 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 user presses 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”>

Get Method– Forms in PHP

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

Forms in PHP - PHP Form example with DatabaseFirst line of 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 user to enter numeric value. Here is the code to get the values of this form when clicked on submit button

Forms in PHP - PHP Form example with DatabaseI entered sample values of John and 31 and here is the output

Forms in PHP - PHP Form example with DatabaseNoticed the URL filed? This is what the problem is with get method. Now let’s jump to a secure method.

Post Method– Forms in PHP

Leave everything at it’s 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

Forms in PHP - PHP Form example with DatabaseThis is the output when selecting 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.

Forms in PHP - PHP Form example with DatabaseFinal Words

So another exciting tutorial comes to an end here. We hope you enjoyed learning that. This series of tutorials will now be directed to a start of completely new website. We will built this website from scratch using all this knowledge and with upcoming. In next tutorial we will built a sign up page for that website and integrate it to MySQL database. Stay connected and let us know your suggestions through comments.

Part One: HOW TO RUN YOUR FIRST PHP SCRIPT ON WINDOWS
Part Two: BASICS OF HTML5 ON THE WAY TO PHP LEARNING
Part Three: PHP WORKING WITH DATA TYPES AND VARIABLES
Part Four: IF ELSE STATEMENTS AND ARITHMETIC OPERATIONS IN PHP
Part Five: TUTORIAL ON TYPES OF LOOPS IN PHP – PHP LOOP TYPES
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Like this post:

One thought on “Forms in PHP – PHP Form example with Database”

Leave a Reply

Your email address will not be published. Required fields are marked *