On Thu, 16 Sep 2004 13:02:31 +0100, Harlequin
<[EMAIL PROTECTED]> wrote:
> Hi all.
> 
> Hoping this might be relatively easy...
> 
> I'm wondering if I can create a dropdown menu (<option>ABCDE</option>) by
> using a select statement and then populating this using PHP...?
> 
> --
> -----------------------------
> Michael Mason
> Arras People
> www.arraspeople.co.uk
> -----------------------------

for a recent project, I needed an easily adaptable form, with a lot of
different, often changing elements, so I came up with the following
function (amongst others!):

// dropdown boxes
function dropdown($fieldname, $currentvalue='', $onchange='') {
       $var_val = $fieldname . '_val';
       $var_disp = $fieldname . '_disp';
       global ${$var_val}, ${$var_disp};
       if($onchange <> '') $onchange = 'ONCHANGE="' . $onchange . '"';
       echo('<SELECT NAME="' . $fieldname . '" ' . $onchange. '>');
       for($loopy=1; $loopy<=sizeof(${$var_val}); $loopy++) {
               echo('<OPTION VALUE="' . ${$var_val}[$loopy] . '"');
               if($currentvalue == ${$var_val}[$loopy]) echo(' SELECTED');
               echo('>' . ${$var_disp}[$loopy]);
       }
       echo('</SELECT>');
}

I have an include file with the options for each form element -
meaning options can be added or removed easily - an example is:

// Sex
$Sex_val[1]                                     = 'M';
$Sex_disp[1]                                    = 'Male';
$Sex_val[2]                                     = 'F';
$Sex_disp[2]                                    = 'Female';

The function would be called by:
// create dropdown
dropdown('Sex');

// create dropdown, select a matching value
dropdown('Sex',$current_sex);

// create dropdown, select a matching value and run a javascript
function when element is changed
dropdown('Sex',$current_sex, 'checkSex()');

HTH

j
;-}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to