PHP Working with Forms

PHP Working with Forms

Last Updated on Mar 21, 2023

Html Form

Today we are going to learn about handling the form in php so it’s important to know the different sections of a form.

Each Html form has an action and a method and a number of inputs with name, value and type.

Form Action

Where this form is going to be submitted to. If it’s the current url leave it empty but if it’s going to be to another url you should specify it

For example:

will submit the form to handle.php in our website          
will submit the form to google (yes it’s possible!)

Form Method

This can be get or post. In very simple terms, If it’s get you can see the inputs in the url (URL parameters) but if it’s post you can’t see it there (HTTP POST).

For example

google .com/search?q=searchhere
is the result of a form with get method And almost all the login pages use POST method because it’s not safe to show the user pass in the url

Input Type

The type of input doesn’t affect our form handling on the server.

Input Name

Later we get all the inputs as an array. Names are the keys in our array

Input Value

If names are the keys then input value is the value of our array

Here we have a form in index.php

    <title>Form Handling in PHP</title>
<form action="./handle.php" method="get">
    Name: <br>
    <input type="text" name="name"><br>
    <input type="submit">

If we write amir in the name and submit it will go to this url: localhost/handle.php?name=amir

we can see that there is an input with the name “name” and value of “amir”

Now let’s go to the handle.php and learn how to handle the request when the form is submitted

Handling Form in PHP

In php there are two superglobal variable that we use to get the key values from the form

  • $_GET
  • $_POST

If the method of the form is get we use the $_GET and if the method is post we use $_POST

Here since our form method is get we use $_GET

First check if the value has been passed and Then you can do anything you want with it. Here we say hello to that person

    echo 'Hello ' . $_GET['name'];
    echo 'Hello Stranger!';

I personally prefer using empty rather that isset because now if I go to localhost/handle.php?name=

The name isset but it’s empty and our output would be


But if I change it to empty since there is no value for name the output will be

Hello stranger!

So I would do it like this:

    echo 'Hello ' . $_GET['name'];
    echo 'Hello Stranger!';

ALWAYS make sure you validate the data. Forms are one of the easiest ways to hack a website. Be very careful with what you do with the data you get.


Now you know about working with forms and handling them in PHP.

I recommend you to open a PHP files and create a form and try to handle the form submit with PHP. change the form to POST see the difference with GET.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • working with forms in PHP
  • handling GET and POST request parameters

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts