Processing forms in PHP

For this article, I will assume that you (the reader) know what a form is and have a general idea of what needs to be done on the HTML side to submit a form. I will cover a bit about what needs to be done but I will not go into details (it will be enough for you to get through form submissions :))

Among all the languages I have come across processing a form in PHP is the easiest. PHP was made with web development in mind and it shows in its ease of use.  PHP provides you with 3 variables that can be used to access the information sent through a web page. They are :

  • $_POST
  • $_GET
  • $_REQUEST

These variables are already present when PHP’s code is run. You don’t have to do anything special. $_POST contains all variables sent through a post request. $_GET contains all the variables sent through a GET request (in a query string). $_REQUEST contains the information sent through both post and get(query string).

Let’s take a look at an example shall we. Here is an HTML page with a form :


Name:
Email:
Message :

All the form elements are enclosed in between <form> and </form> tags. The attribute action=“index.php” lets the browser know where the form is to be submitted. In this case, it is submitted to index.php. And, method=”post” lets the browser know that the method being used to submit the form is post (and not get). In every form you need one “button” or image that will submit the form. In the above code, it is a button. Now that we know the form we will use to submit the information. Let’s take a look at the PHP page which will process it.


The code is very simple and straight forward. It takes all the information sent through the HTML form and emails it to [email protected]. In the HTML form, we had an input field with the name of message :


You can access the value of this field in PHP through the variable : $_REQUEST[‘message’]. If you used the method post (like I did in the code above), you can also access the value of the message field through the variable $_POST[‘message’]. However, $_GET[‘message’] will not contain anything as the method we used in our form was post and not get.


Posted

in

by

Comments

Leave a Reply

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