Easy Tutorial How to Make HTML Forms using PHP Programming Language

Forms are easy to work with when building a website. In fact, you need to know a lot of details. I took a close look at the HTML Forms tutorial in the HTML tutorial series, but I haven’t had a chance to learn how to actually handle the data collected from the form.

We are currently working on a large series of PHP tutorials, and now is a better time to take a look at form processing with PHP than right now. I found out that links and urls pass data via GET super global. In this installment, we’ll take a look at the POST superglobal and see how it relates to form processing in PHP. Let’s go now!

The ways to get user data

  • Links and URLs You can use a query string to pass key/value pairs from your data via a GET super global.
  • Forms In HTML forms, POST is a common method of transferring data and does not require encoding / decoding like links and URLs.
  • Cookies Setting cookie values with PHP allows you to read and write data to associative arrays as needed.

In this adventure, I am specifically targeting the second approach using a PHP POST request and an HTML form. You can access the data submitted by the form using the $ _POST superglobal.

Various fields in the HTML form have a name attribute, which will be the key of the associative array in $_POST. You can use form processing through a two-page process or a one-page form process. Each has its pros and cons, but for now, let’s take a look at the two-page approach. This means that the first page will contain the HTML markup for the form.

This form has an action attribute, and the value of this attribute is the second PHP page that will process the data. The easiest way to see how it works is to write it in code.



So, there are two page settings for the most basic form handling. When you load built.html into your browser, you can enter information into the form. Enter https://www.alfintechcomputer.com in the first field and Social Media in the second. Then hit the submit button and see what happens.

information array

Very cool! You can see how the name property became the key of the array and the entered data became the value of a specific key. This data was transferred from page 1 to page using the POST method. Let’s see how to get data from this array and actually use it.

In real programs, there are restrictions on how you process this information. I’ll take two values and put them in a variable, and then somehow use them in the page. Let’s update the process_form.php file as follows and see what you can do.


Hi bud, thanks for submitting https://www.alfintechcomputer.com to the Social category.

Detecting Submission Form

Now that you understand how to create a basic HTML form on one page and a PHP script on another page, let’s see how to determine if there is a form submission. This is a common and necessary step when processing forms in PHP. There are many reasons for this, but one of the main ones is when the user tries to directly load the form processing page, in which case the application throws an error. So what’s the best way to do this? Let’s take a look.

Set Default Values

The only thing we can do is adjust the logic to assign default values, if needed. This way, if the user submits the actual form processing page instead of the HTML form itself, the values can be set to reasonable values as needed. You can easily do this with the ternary operator.


When I load this page myself, I get a “Thank you for submitting http://twitter.com to the social category”, but when I submit a blank form from built.html, the processing page continues to run on load, but the value remains blank. There is.

The best way to determine the shape

The previous example is a start, but there is an easier and better way. Let’s change the form processing script to the following format.


This is great and now offers more depending on the line you’re looking for. When the user submits valid data to the form and then clicks the submit button, it might look like this:

Nice Form Submission!
Hi bud, thanks for submitting Google+ to the Social category.

However, when the user tries to load process_form4.php directly, it throws this message.

Stop trying to hack the form fool!

What we do is check if the $ _POST array has a submit key. The only time you have a key is when the user actually clicks the submit button on the form, so this is the easiest way to detect a form submission.

Single Page Submission Form

Another way to process forms and submit forms in PHP is to end the form processing on the same page as the HTML form. Let’s see how to do it.


Now when this page is loaded initially or a blank form is submitted, the user is prompted to enter a website and category. Thank you for adding http://facebook.com to the Social Media category.

When users submit something like http://facebook.com and Social Media and then click Submit, you will receive the same message as hello buddy.

I added only minimal validation to the if clause and skipped user input through html special chars, which I learned in the htmlentities vs htmlspecialchars tutorial.

Basic Form Validation

Today, using a PHP framework like Laravel, Symfony, Zend, Codeigniter, Yii, or many other fantastic solutions is the way to do things like form validation. It is still important to know the basics of how these frameworks do their magic, and for that you need to understand PHP itself at the most basic level. If so, what does he usually confirm? Let’s take a look.

Validation Characteristics

  • Presence Has the user really entered anything in this field?
  • String Length Was the string long enough to warrant a valid value?
  • Type Check very often data types correction.
  • Inclusion If you have a list of 10 values, does that value give one of the 10 possible values
  • Uniqueness Is the data submitted by the user 100% unique? This is a common occurrence when sending email addresses.
  • Format Does your website address contain http://, or does your email address contain an @ sign, etc.?

This is the adage of the iceberg because data validation can have hundreds of characteristics. But remember the Young Jedi, we’re talking about the basics of this episode. Now let’s see how these types of validation rules look like in basic PHP.


Validation Functions

When you are not using the complete framework, it is best to put your validation logic in reusable functions. Here are some examples you can use to test your form validation on one page.


Now you can embed this file directly into the form page as follows:


It’s incredible! Now, on the same page, I have a fully functional HTML form that is processed through PHP and handles the validation with the validation function and displays an error in a nice list format if something goes wrong.

Leave a Reply

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