This is a simple tutorial on how to handle forms in PHP. I wasn’t planning to post anything this basic on the website, but since then, have come across a number of forums where newbies have posted questions regarding handling form submissions. I guess this is the most common obstacle faced by most new comers to PHP.
Before continuing with the article, download the PHP form handling sample code.
Anatomy of HTML form elements
Take a look at the source code of form.php file. As you can see most HTML form elements have three attributes in common.
HTML form tag
The form tag has two attributes which are of interest to us. The method attribute and the action attribute. The method attribute can have two values “POST” and “GET”. The main difference between the two is how the data is sent when the form is submitted.
In the GET method, the data is sent as a URL string. The amount of data that can be sent is limited and this limit depends on the browser being used. For Internet Explorer this limit is 2Kb. That is, you can not send more than 2048 characters through Internet Explorer using GET method.
In the POST method, the data is sent as a part of the request to the server. That is, it is embedded in the message body. There are no limits to the amount of data that can be sent.
A search for “POST vs. GET” on Google will give you a lot of useful resources on the usage and difference of the above two methods.
Simple Rule of Thumb
Use POST method for data manipulation: Adding and editing data.
Use GET method for displaying data: Searching, sorting, pagination etc.
The action attribute simply points to the script that will handle form submission. In this case, it is form-exec.php
Form handler script
We will use the element’s name to access its value in PHP. For example, $_POST[“pname”] gives us the value of a form field named “pname”.
If we were using GET as the submission method, we will need to use $_GET[“pname”] instead of $_POST[“pname”].
You can get any form elements value by using the $_GET or $_POST arrays.
//To access the value of a field named "gender"
//If using POST method
$gender = $_POST['gender'];
//If using GET method
$gender = $_GET['gender'];
Select list with multiple selections
Handling of select lists with multiple selections is slightly different from other form elements. In the example form, the interests select list allows multiple selections and hence requires special handling.
If we name this select list simply as “interests” and try to retrieve its value using $_POST[“interests”] we will only get the last selected option. We need to name this select list as interests . This tells PHP to treat the values coming from this element as an array.
Now, $_POST[“interests”] will give us an associative array which is populated with the selections made by the user.
Suppose you want to allow the user to select multiple checkboxes as shown in the image below. The logic we applied to the select list above will also apply here. All the checkboxes in the group will have the same name but will have different values.
All the checkboxes should have the same name but append  to this name. For example, utilities. Now, $_POST[“utilities”] will give us an associative array which will be populated with the values of the checkboxes selected by the user.
Note: Only the name attribute is important here. You can set the id attribute to any value you want. You need not append the  to the checkboxes ids. Moreover, the ids need not be same.
<input name="utilities" value="Community Fees" type="checkbox" id="commfees">Community Fees <br />
<input name="utilities" value="Water" type="checkbox" id="water">Water<br />
<input name="utilities" value="Electricity" type="checkbox" id="electricity">Electricity<br />
<input name="utilities" value="Gas" type="checkbox" id="gas">Gas
- Variables are case sensitive in PHP. $_POST[“pname”] is not same as $_POST[“PNAME”]
- Value of checkbox is available only if its checked