loader image
Skip to content
Menu

Note: Some of these articles assume a basic knowledge of PHP, HTML, JavaScript, CSS, and MySQL. If you are unfamiliar or need to brush up, useful tutorials and information can be found at W3Schools and Codecademy.

One of PHP’s main functions is to serve as a relay between the server and the web browser, meaning that it can transfer database information to the web browser as well as take user inputs and store them in a database. In order to store user information into a database, there are several steps that need to be taken: retrieve the users information, process the users data, and lastly send the data to the database. This article will focus on how we can use PHP to retrieve the user information, and it is done through the use of a ‘form’.

A form is exactly what you think it is. It’s specified areas on a website that a user enters information and then submits. Any time you have ever signed up or logged into a website, the website takes your inputs and sends them to be processed thought the use of a form. So now the main question, how do we make one? As it turns out, it’s actually pretty simple. Obviously, as with anything, as we progress things can get a little complicated, however, the concepts still remain the same. There are thee parts that will go into creating our basic form: creating the form with HTML, specifying our user inputs, and creating an action to send the information. so for now let’s stick with the basics and get started.

Creating the form with HTML

The first step to any form is creating it with HTML. You may be asking, if we are using PHP for the form, why are we making it with HTML? The answer to this is that PHP doesn’t actually make the form. The form is created using HTML, and is sent via a PHP ‘POST’ function. For more detailed information on this check out our article “Getting Started With PHP:  An Overview and What It’s Used For“. So, let’s create a basic login form with HTML.  Create a web project in our ‘htdocs’ called ‘myProject’, and inside that let’s create a file called ‘login.php’. Open ‘login.php’ inside a text editor and write the code…

form

This is all we need for a basic form. In the next sections we will take a look at the user inputs, and then explore how to send the form using PHP. So, let’s break down what is going on in the user inputs and how they will be used when we submit the form. 

Specifying our user inputs

Now that our form is created, let’s start looking into our user inputs. In our example, we have two: username and password. Notice that these are two different ‘types’. There are many different types of inputs, and we won’t really get into it here. If you want to know more there is a reference of all the different input types here. For now just know that the type attribute allows us to specify what type of input we want. The other part or the user input that is important is the ‘id’. The ‘id’ attribute is important because when we submit our form, the page that we send it to will recognize the data by it’s id. We send the information via the ‘POST’ method, when we do so the input id’s essentially become POST variables, and are referenced via $_POST[input id] on the page the form is sent to. So, now we have created our form and specified our user inputs. The only thing that is left to do is to create the action to send the form.

Creating the action to send

When creating the action to send the form, there are two parts. The submit button and the action attribute. The action attribute will require to add just a little bit of code to our form, so let’s start by looking at the submit button. The submit button functions exactly how you think it would. it is associated with the form element it is embedded within, and the form is submitted when the button is clicked. It’s pretty straight forward, but when we click it, how does the form know where to go? This is where the action attribute comes in, and there are two parts to it: the ‘action’, and the ‘method’. The action specifies the path to the destination file that the form will be sent, and the method specifies the way that the form is sent, either through ‘POST’ or ‘GET’. For the sake of this example we are going to use the ‘POST’ method. So, in order to include these into our form we need to change our code to…

form

Looking at our new code, what has changed? Our form now has a destination of ‘submitLogin.php’ and will be sent through the ‘POST’ method. That’s all we have to do; our form is now ready to be submitted. Going forward we may have more inputs and of different types, but in the end the same process will be used for every form we make.  

However, as we stated earlier, when we are sending user information to the server, submitting the form is just the first step. We still need to process the inputs on the page we send the form, and then send them to the server. You can find out more about these steps soon on our upcoming Getting Started With PHP articles “Processing Form Information”, and “Sending User Information to the Server”, and our upcoming tutorial project “Creating a Basic Login”.