Using forms in PHP: setting up the form

A form is an area of a webpage that gives the user places to enter information, whether it is in the form of a box they can type text into, a dropdown list, a set of check boxes or something different. These individual parts of a form can be called controls, and in HTML terms they should all fall within the opening and closing and tag. A brief guide to writing HTML forms is available from w3schools.com.

Form actions

The first thing to understand is that generally each form has an "action". This is an instruction as to what to do when the user presses the submit button at the bottom of the form. This is specified as the "action" attribute of the opening tag. The simplest example is that the user gets redirected to a new page, which is exactly what we want here. For this, the action should just be set to the name of the new page.

Another attribute you need is the method by which the form will be submitted - cunningly named "method". Here you can either use "get" or "post". The differences between the two are rather over-lengthy to explain here, but suffice to say either will generally work for very simple and safe applications. The W3C recommendations suggest that if your form is going to have permanent side effects (e.g. order a book, upload a picture, add a message to a guest book) it should be of type "post". Otherwise, if for example it is simply going to retrieve a list of search results but won't actually change anything on the server, use "get".

So now we know what we need to include in the form tag, here is an example.

<form name="searchform" method="get" action="search.php">
<!--   all the code to build your form goes here including the below submit button-->
<input type="submit" name="Submit" value="Submit" />
</form>

This means that when the user presses the submit button which you will have to define later, they will be taken to a page called search.php. The form data will be transferred across using the "get" method. For the sake of completeness, it is important to give your form a "name" in the above way too - you may never use it but it gives your pages a way of identifying a particular form if you need to later. Don't include two forms with the same name on the same page.

Form Controls

You then need to place some controls onto your form using the HTML syntax for doing so. We're assuming you know most of how to lay this out already. Two key things to keep in mind are that each control on a form, be it a textbox, a dropdown or a checkbox has both a name and a value.

The "name" should be unique to that form so no two controls have the same name. It provides a unique way of identifying it which you will need if you want to use its contents later. You set the name when designing the form by including it as an attribute in the opening <input> tag of each control.

The "value" is generally relates what the user has chosen to enter into the control. For a textbox, the value is simply the line of text that the user wrote into that control. No effort is required here on your part.

Hence, for a textbox, the following code would be an acceptable (if rather basic) way of defining a textbox that allowed a user to search for something.

<input type="text" name="searchfor" />

The textbox's name will be "searchfor" and its value will be whatever the user chooses to enter into it.

For less straightforward controls things aren't quite so obvious. For example, the user does not type anything into a dropdown list or checkbox but merely chooses one option. Therefore we cannot refer to its value in quite the same way.

The solution is however quite simple. Whilst designing the form, the programmer clearly know what the choices are going to be. At design time, we can therefore use the "value" attribute of the <input> tag to assign, behind the scenes, what each option will mean to the page it is passed on to. The following example illustrates a dropdown box allowing a user to pick their favourite fruit.

<select name="fruit">
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>

The above code all relates to a single control. Remembering the two key parts of the control, the name of the control is "fruit" and the value will either be "1" if the user selected and apple or "2" if the user went for a banana.

Checkboxes open up another way of transmitting information. As far as PHP is concerned, if a simple checkbox is ticked when the form is submitted, its name will be available as a variable to the next page. If not, then it won't exist in any way. We can use this knowledge on the second page to check for whether or not a box was ticked by the user before hitting submit by using the PHP function "isset". Therefore you can use a checkbox in a similar way to a textbox when designing a form:

&lt;input type="checkbox" name="emailme" /&gt;

would be a reasonable declaration for a checkbox that let the user decide whether they received email or not.

The name of this control is "emailme" and the value would either evaluate (by default) to "on" if it had been ticked or not exist at all if it had not been ticked.

Addendum: you can actually set a value to any type of control, you just do not have to.

<input type="checkbox" name="emailme" value="sendspam" />

would be just fine. In this case the control would be called emailme, and its value if it was ticked would be "sendspam".

Next section - using the form's information

Back to start - Using forms in PHP