How to Create PHP Web Form


Introduction

Almost every web contains a form nowadays. These forms basically function such that they submit the data entered in a separate file or a database in an organized and logical order. In this tutorial, we will teach you how a PHP web form can be created

PHP web forms are similar to simple HTML forms; the only difference is that they use PHP script in the action method along with the transaction mode.

We can use any editor for the code but for the purpose of this tutorial, we will be using notepad ++ as our code editor.

Step 1 – Form Fields

To start with the PHP web form, we have to first specify the php script in the form action tag and then the data transaction mode. For the purpose of this tutorial, we have specified the script as “welcome.php” and the transaction mode is “post” as we have to submit the data.

With that done, we created a name field, set its input type as text and assigned its logical name as “fname”. Similar pattern was used for another field which goes by the name of age.

Next, we will set the input type as submit. After that, we will close all the tags of form, body and html respectively.

Once done with that, save the file as Html document and close it.

Coding the Form

Step 2 – Defining the Script Functionality

Now, we will create a new file for welcome.php. Start with html and body tags.

After that, use the echo function with the “$_ post” function and specify fname in the argument. The “Welcome” string is just used for concatenation which will just insert a Welcome statement with the name which will be inserted in the name field.

Repeat the same process for the age field. This time, however, use “You are” as text for concatenation.
Once done with that, save the file, but this time choose “PHP” as the file type. Name the file as “welcome” because we have specified the same name in the previous HTML code where the form was being coded.

Coding the Script

Step 3 – Enter the Values

Now open up the Form.html file in the browser. There would be two fields of name and age. Test the form by inserting values in the fields and then click on the Submit button.

A new page will open where the output would be printed. In this manner, we can create PHP web forms.

Test the form