Categories

Syndicate

HostGator.com

Handling forms in PHP

Listed under

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 newcomers 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

Like I mentioned earlier, we will use the element’s name to access its value in PHP. For example, to access the value of field named “pname”, we will use $_POST[‘pname’].

If we were using GET as the submission method, we can get the value of the “pname” field as $_GET[‘pname’].

You can get any form elements value by using the $_GET or $_POST arrays.

<?php
	//To access the value of a field named "gender"
	$gender = $_POST['gender']; //If using POST method
	$gender = $_GET['gender']; //If using GET method
?>

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.

Checkboxes

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.

<html>
<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
</html>

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

Comments

Mike Baran Jan 02, 2008

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

Post your comment
All comments are manually verified. So don't waste your time posting spam.

  Textile Help

Related Articles

Digg this article Bookmark on Delicious Add to your Technorati Favorites StumbleUpon Add to BlinkList Simpify This

More PHP Articles

Turnkey Templates