9.3 Form Objects
9.3.4 Select Box
The Select Box is one that can benefi t more than probably any other from PHP assistance, as we shall soon see.
The HTML used to produce the Select Box next to the word Nationality in the fi gure at the start of the chapter is given below.
The value displayed, in this case British, is the default value. Clicking on the small box next to it (or other symbol such as an arrow, depending on the browser used) produces a short drop-down menu. There is a choice of four nationalities, each of which will cause a value to be sent to the destination page. Thus choosing 'Chinese' will result in the value CN being sent.
A default value can be specifi ed by using the 'selected' attribute of the <option>
tag. For example
If none of the <option> tags has a 'selected' attribute, the fi rst of them is assumed to be the default.
The main diffi culty with using a Select box is that there will often be a large number of alternative options. Suppose we want a user to enter a date of birth in the order day, month and year. We need a Select box for day with values from 1 to 31, another one for month from 1 to 12 and one for year of birth from (perhaps) 100 years ago up to the current year.
The PHP function wfselectNumrange will generate a select box for a specifi ed range of numbers.
136
Calling the function by
will generate 33 lines of HTML
(The lines for values 4–28 have been replaced by a row of dots to save space.) Here value 2 is the default. Passing a fi nal parameter which is outside the speci-fi ed range, e.g. zero will ensure that the 'selected' attribute is never included, with the result that the fi rst value listed is taken as the default.
It is not essential for the value and the text shown to the user to be the same but in case of a day of the month there seems no reason for it not to be.
A select box for 'month' with no default can be generated by the function call
We can then use the two lines of PHP
to generate a select box for 'year' with 103 lines of HTML, with no default.
We may wish to improve on this in the case of the select box for month, by dis-playing the words January, February etc. to the user whilst retaining the values 1, 2 etc.
To do this we will create a separate function to generate a select box as follows.
9 Passing Variables to a PHP Script I
Calling the function by
will produce 14 lines of HTML with February as the default month.
Now we have the wfselectNumrange and wfselectMonth functions we can easily write the PHP script needed to generate select boxes that can be used for inputting the date of an event we are organising, assuming it is sometime this year or in the two following years. We will make the default date displayed today's date. We can do this by:
If today were July 4th 2018, the result would be a row of three select boxes like this.
138
Two further attributes that can be used with the <select> tag are size and multiple.
The size attribute might be better called 'height'. The combination size=2 speci-fi es that the select box should be two values high rather than the usual one. The HTML below
gives a form object that looks like this.
This is unlikely to be of much value and has the possible disadvantage that if no option is specifi ed as checked, there will be nothing sent to the destination page, rather than the fi rst item in the normal situation where size is not specifi ed (or has the value 1).
The real value of the size attribute is when it is used in conjuction with the mul-tiple attribute. To illustrate this we will change to a new example. The HTML below generates a select box which enables the user to specify any from zero to all seven choices in a list of possible interests.
There are three points to note about the <select> tag
9 Passing Variables to a PHP Script I
The fi rst is the use of the attribute 'multiple'. The second is the use of the 'size' attribute. This gives a larger (taller) selection box, which makes it easier to make multiple selections. The third and most important point is that instead of a name such as interests the name is given as interests[], with the opening and closing square brackets indicating that an array of options will be transferred to the destina-tion page, rather than a single value.
The form element looks like this.
To make multiple selections click on the fi rst selected item. This should cause it to become highlighted. Now hold down the 'Ctrl' key on the keyboard and click on the second and subsequent choices, which should all become highlighted too.
Clicking on the same choice twice will cancel the selection. (For some keyboards, it may be that some other key needs to be used rather than 'Ctrl', possibly 'Alt'.)
We end this section by showing how to create a select box when there are a large number of options, which do not form some simple sequence such as numbers from 1 to 31. We will illustrate this by showing how to construct a select box for country of residence.
The International Standards Organisation (ISO) has a list 1 of around 240 coun-tries, each with a corresponding unique two character code: FR for France, GB for United Kingdom etc. To enclose these country names and codes in HTML <option>
tags would be an extremely tedious and error-prone task. It is much easier by means of a little cutting and pasting to create a text fi le, which we will call countries.txt containing 240 or so lines such as
and ending with
1 Currently located at http://en.wikipedia.org/wiki/ISO_3166-1
140
Here each country name has been separated from its two-character code by an asterisk. The United Kingdom has been moved to the top of the list as the author's preferred default country. The list may also have been cut down to a more manage-able one of say 100 'major' countries plus a line such as
Other*ZZ as the fi nal entry.
Now all we need to generate a Select box for countries is the following PHP function.
which we can call by
The 'for' loop (lines 4–8) and the assignment statement in line 5 isolate each separate line of the text fi le. Then the use of the explode function (line 6) divides the line into the parts before and after the asterisk.
Now the wfselectlist function is available it can be used to generate other lengthy lists in a simple fashion from a text fi le, in the same way.
9.3.5 Checkbox
The checkbox next to the words 'I agree to the terms and conditions' in the fi gure at the start of the chapter can be generated by the HTML
If the box is checked by the user the value 'terms' for variable 'tsandcs' will be passed to the destination page.
9 Passing Variables to a PHP Script I
To make the default value that the box is checked we use the 'checked' attribute.
In this case a tick (or similar symbol) will appear in the box in the web form.
The following PHP function will generate this form object.
To generate a checkbox where the box is checked by default we can use a PHP function call such as:
To generate a checkbox where the box is unchecked by default we can use a PHP function call such as: