Index |
||
Methods | ||
baaSelect | Create new baaSelect object | |
addSelect | add <SELECT> component to the object | |
makeScript | Generate the javascript data arrays and function code | |
makeSelect | Generate html code for the <SELECT> component | |
Code samples and tutorial | ||
Download class | ||
Download guide |
Method Reference |
||
![]() |index| |
baaSelect object baaSelect ([dbtype[,dbConnection]]) | |
dbtype | [optional] the type of database you are using. Default is MySQL | |
dbConnection | [required for ODBC only] the link resource returned by odbc_connect() | |
|
||
![]() |index| |
addSelect(selectName, srcTable, idField, descripField [, fkField [, order [, defaultText]]] ) | |
selectName | Name to be give to your dropdown | |
as in <SELECT name="selectName"> |
||
srcTable | Table containing the data used to populate the dropdown |
|
idField | Field in srcTable to be used as the option values |
|
descripField | Field in srcTable to be used for the dropdown display text |
|
fkField | [optional] Foreign key field in srcTable which links the option to the parent dropdown's selected value | |
Always empty in the first dropdown added. If it is left blank in other drowdowns it will indicate that it is not a related dropdown and the values will be fetched from the srcTable and it will not be updated when parent value changes |
||
order | [optional] Sort order for option items | |
Can be 'BY_ID' or 'BY_TEXT'. Default is 'BY_TEXT' |
||
defaultText | [optional] Text display when no values are selected. Default is blank | |
eg |
||
![]() |index| |
makeScript() | |
No parameters | Place this method call in the 'head' section of your page to generate the required javascript code | |
<head> <? $sel->makeScript() ?> </head> |
||
![]() |index| |
makeSelect(selectName) | |
selectName | Name of the select dropdown to be generated | |
<form method='post' action='some.php'> <? $sel->makeSelect("carmake"); $sel->makeSelect("carmodel"); ?> </form> |
Data | CREATE TABLE `region` ( `regionID` int(11) NOT NULL auto_increment, `region` varchar(20) default NULL, PRIMARY KEY (`regionID`) ) TYPE=MyISAM; INSERT INTO region (region) VALUES ('East Anglia'), ('London'); CREATE TABLE lea ( leaID int not null auto_increment primary key, lea varchar(20), regionID int) ; INSERT INTO lea (regionID, lea) VALUES (1,'Norfolk'), (1,'Peterborough'), (1,'Suffolk'), (1,'Cambridgeshire'), (2, 'Barking and Dagenham'), (2, 'Barnet') ; |
Sample PHP code | <? $cnx = mysql_connect('localhost'); mysql_select_db('test'); include 'baaSelect.php'; $sel = new baaSelect(); $sel->addSelect('region', 'region', 'regionID', 'region','',1,'--region--'); $sel->addSelect('lea', 'lea', 'leaID', 'lea','regionID',1,'--select--'); ?> <HTML> <HEAD> <meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)"> <title>Sample</title> <? $sel->makeScript() ?> </HEAD> <BODY> <form method=get> <? $sel->makeSelect('region'); # this creates the HTML $sel->makeSelect('lea'); ?> </form> </BODY> </HTML> |
Generated HTML code | <HTML> <HEAD> <meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)"> <title>Sample</title> <SCRIPT language="JavaScript" type="text/javascript"> <!-- var currentregion = 0 ; var currentlea = 0 ; var arrayleaTXT = new Array(); var arrayleaVAL = new Array(); arrayleaTXT[0] = ""; arrayleaVAL[0] = 0; arrayleaTXT[1] = "Cambridgeshire|Norfolk|Peterborough|Suffolk"; arrayleaTXT[2] = "Barking and Dagenham|Barnet"; arrayleaVAL[1] = "4|1|2|3"; arrayleaVAL[2] = "5|6"; function baaSelectUpdatelea (form) { currentregion = form.region.options[form.region.selectedIndex].value; while (form.lea.options.length) { form.lea.options[0] = null; } var tmp = new String (arrayleaTXT[currentregion]); var arrayText = tmp.split("|"); tmp = new String (arrayleaVAL[currentregion]); var arrayVals = tmp.split("|"); var optionlist = form.lea.options; optionlist[0] = new Option(); optionlist[0].value = 0; optionlist[0].text = "--select--"; if (arrayleaTXT[currentregion]) { for (var i=0; i<arrayText.length; i++) { optionlist[i+1]=new Option(); optionlist[i+1].value = arrayVals[i]; optionlist[i+1].text = arrayText[i]; if (currentlea == arrayVals[i]) optionlist[i+1].selected = true; } } } // generated by baaSelect.php --> </SCRIPT> </HEAD> <BODY> <form method=get> <SELECT name="region" onchange="baaSelectUpdatelea(this.form)" > <OPTION value="0" > --region--</option> <OPTION value="1" > East Anglia</option> <OPTION value="2" > London</option> </SELECT> <SELECT name="lea" > <OPTION value="0" > --select--</option> </SELECT> </form> </BODY> </HTML> |