Handling forms in PHP

August 6th, 20062 Comments ยป

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.

  1. name
  2. id
  3. value

The “id” attribute is not required right now but will come in handy when you start using JavaScript. Each element should have a unique name and id. We will use the element’s name to access its value in PHP.

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.

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.

Multiple checkboxes

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.

Note

  1. Variables are case sensitive in PHP. $_POST[“pname”] is not same as $_POST[“PNAME”]
  2. Value of checkbox is available only if its checked
« Prevent Duplicate Form Submission
PHP FTP functions tutorial »

Categorized Under

PHP Basics PHP Form Handling

About jatinder

» has written 17 posts

2 Comments

  1. Mike Baran says:

    Great technical suggestions. Thanks (from someone new to PHP but not to programmming).

  2. Robert says:

    Informative as to how to load multiple selections into arrays. I have an array that contains information about my fields, and I would like this array to be available in my form handling procedure. Do you have any suggestion as to how to do this?


*