PHP Form Tutorial & Example: $_GET and $_POST
Receive new PHP tutorials.
What is an HTML Form
An HTML form is used to get information from the users visiting a website. Technically, it's an HTML tag used to create an element with various types of controls such as simple inputs, text areas, check and radio boxes, labels and buttons, etc.
You can either submit the input from forms to a server for processing or email it.
You can create a form in HTML using the
</form> tags. Inside the area defined by the opening and closing tags, you can use controls to create the elements of the form, depending on what information you need to get from users.
In this tutorial we'll learn about:
- How and why we use HTML forms in PHP
- How to create a form in PHP
- How to use the POST method
- How to use the GET method
- How to build a simple login and registration form in PHP
Why Using HTML Forms
Nowadays, most web apps need a way to get input from users. Here comes the role of HTML forms which are used to enable users to submit input to the HTTP server.
You can use forms for handling multiple tasks such as:
- Enabling users to register (create new accounts) and login
- Inserting data into a database system and make CRUD operations,
- Submitting queries for searching a database etc.
How to Create a Form in PHP
Actually, you creat a form in HTML which can be embedded in the PHP code.
You can create a form in HTML by using the
- You then need to add the input controls,
- Specify the type of the HTTP method to that will be used to submit information to the server,
- Add the submit URL that will be used to process the form data,
Let's see a simple example form.
Start by creating a project folder using the following command:
$ mkdir php-form-example
Next, navigate inside the folder and create an
index.php file and add the following content:
<html> <head> <title> Register Form</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h2 id="logo">Register Form</h2> <form action="register.php" method="POST"> <label for="fullname">Name:</label> <input type="text" name="fullname"> <br/> <label for="email">Email:</label> <input type="email" name="email"> <br/> <label for="password">Password:</label> <input type="password" name="password"> <br/> <input type="submit" value="Register!"> </form> </body> </html>
<form> tag has an action attribute with the
register.php value which means our form will be submitted to th
/register.php URL to be processed via the POST method specified in the
We have added controls for creating labels, input fields and submit button.
Now, let's use the PHP builtin development server to start a server at
127.0.0.1:8000. Simply run the following command from the folder of your project:
$ php -S 127.0.0.1:8000
Using your web browser, navigate to the
http://127.0.0.1:8000 address. You should see the following form:
With some basic CSS styling, we can get a better looking form:
Here is the codepen for the example with CSS styles:
Processing the PHP Form:
action attribute of the form tag we specified
register.php as the URL of file that will process the form once it's submitted. We also specified
POST as the method that will be used to submit the form to the server.
In the project's folder, create a
register.php file and add the following code which will simply returns the data that you have submitted via the form:
<?php echo "You have submitted, name: " . $_POST['fullname'] . ", email: ". $_POST['email']. " and password: ". $_POST['password']; ?>
Since we have specified the POST method in the form, we can get the submitted information from the
The keys in the array are the values specified in the
name attribute of the corresponding input in the HTML form.
If you want to use the GET method to submit data in your form you need to specify
action="GET" in your form. In this case, you can get the submitted data via the
$_GET array instead.
Note: Please note that the form values submitted via a GET method are appended to the URL and are visible to anyone via the browser's address bar.
You can use the PHP
isset()to make sure a value exists in the
$_GETarrays before trying to access it. For example:
Forms are a crucial part of any web application. As such, PHP makes it easy to access the data of submitted forms via the
You can create a form using the
<form> tag and various types of controls.
About the Author
Read our other tutorials
- Angular Tutorial
- Laravel 6 Tutorial
- React Tutorial
- React Hooks Tutorial
- React Native Tutorial
- Webpack Tutorial
- PHP Tutorial
- Django Tutorial
- Java Spring Tutorial
- Rails 6 Tutorial
- Ionic 4 Tutorial
- TypeScript Tutorial
- Bootstrap 4 Tutorial
- Electron Tutorial
- Cordova Tutorial
- HTML Tutorial
- GraphQL Tutorial
- CSS Grid Layout Tutorial
- PWA Tutorial