How to Use HTML Form [PHP] | RainaStudio

How to Use HTML Form [PHP]

Do you know? What is the HTML element that is most used all over the web? Interactive web application without HTML form is unimaginable. Today we are going to learn how to use HTML form at it’s prime and get the best result from it in our daily web development.

Table of Contents

The Basics of Using HTML Form

Mainly , html form collects data from user/visitor and send it to server.

Then what can be done with this simple thing?

Well, when we are able to get user data to our server we can use/verify/save/ that data and also show the user/visitor a result using a server side language like PHP.

Image you have dynamic data which is inputted by a user inside a powerful server side language what not you can do. If you are a code freak you can explore a lot of things.

Any login or registration form runs on the same basics. Any search form runs on same basics. 

As I love PHP most, I will use that by default in this article.

The HTML Form Element

Form element have to be consisted of few input elements and a submit button to act properly according to our needs. Let’s take a look on form important  attributes.

Action: Form action attribute directs the form to a page or file. If we want to send the data to the same page we will need to keep the action attribute value blank..

Method: There is two methods that a form can chose to send data. 

  • POST: Means it will send the data appending inside body of HTTP request. It will not show them in the URL. Very useful to collect sensitive submissions.
  • GET: Means it will send the data appending to the URL of the page. It can be bookmarked and used to send less sensitive data.

Form Method “Get”

I kept the action field blank to target the same page in which the form is currently showing.

When we submit the form we can a url like this

http://localhost:81/test.php?searchTerm=213&in_category=113

The form is collecting and sending data using get method to the server. We can use a server side language like PHP to process the data. Server side PHP can collect the data by using $_GET[‘input-field-name’]. $_GET is a server variable which stored all the data that is sent using GET.

For Example

Now we can use the variables to many type of task.

The form post example is coming soon. If you have any opinion you can share it thorough comments.

Form Method “Post”

Form Post method is used to perform actions that depends on sensitive and confidential data. Login form, Signup Forms and many other forms uses this method to send the data to the server.  See above, when we used method GET the data is being shown in URL whereas when we use POST method it appends the data to the body of that HTTP request and what does this basically mean is it does not show the data to the url.

So let’s see one simple post method form example:

Again, I kept the action attribute value blank so that the form can send the data to the same page in which the form is currently being displayed. Now if we save it in a php file and click submit we won’t see any changes to the URL Well then how do we know the form is submitting data to the server?

There is certainly a way!

After clicking submit if we go to inspect element > network then we can see the evidence that the forms is submitted or not.

HTML FORM POST METHOd

We can clearly see that the data has been set to server. Now if we go back to the test.php file and put something like this.

Server can access POST data using $_POST[‘input-field-name’] . 

Read also: How to Add a Contact Form to WordPress

Conclusion

We have gone through the basic of using a HTML Form using PHP as server side language. If you love to code go and explore the form as far as you can. Because there is no end what you can do with form and form processing. If you found it useful then it’s our satisfaction. Share your thoughts with comments.

Jaber Molla
 

His childhood ​passion for tech helped him to build his wide-ranging experience in back-end web programming. ​He insists on best practice and clean code to avoid vulnerability and other critical problems that arises from poor design of coding.

>