Re: modifying drop-downs
| From: | php3 at developersdesk dot com | Date: | Mon, 14 Aug 2000 05:02:31 +0000 |
| Subject: | Re: modifying drop-downs | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11554@lists.php.net to get a copy of this message | ||
Addressed to: curtpdx@inetarena.com
PHP-general <php-general@lists.php.net>
** Reply to note from curt666 <curtpdx@inetarena.com> Sun, 13 Aug 2000 20:00:56 -0700
>
> Hi all,
>
> I'm having trouble pulling data from a query result into the
> drop-downs on the add/modify form. The form is a mix of fill-in CHAR
> columns and ENUM columns. Add works, the row data returned from the
> query is right, but the drop-downs reflect the initial value in ENUM
> and OPTION VALUE, not the actual row info (the CHAR columns populate
> properly).
If you want anything but the first entry in the list selected, you have
to do it yourself by adding SELECTED inside the <OPTION> tag for the
entry you want checked.
I'm too lazy to write out all the if's all the time, so I do something
like this:
function SelectField( $FieldName, $Options ) {
echo "<SELECT Name=\"$FieldName\">\n";
reset( $Options );
while( list( , $Option ) = each( $Options )) {
echo " <OPTION";
if( $Options = $GLOBALS[ $FieldName ] ) {
echo " SELECTED";
}
echo "> $Option\n";
}
echo( "<\SELECT>\n" );
}
You might call it like:
<?
$Categiories = array( 'literature', 'fiction', 'non_fiction humor',
'history', 'hist_fiction', 'science' );
$Bindings = array( 'paperback', 'trade paper', 'hardcover' );
?>
<? SelectField( 'Binding', $Bindings ) ?><BR>
<? SelectField( 'Category', $Categories ) ?><BR>
<?
If you are displaying data from a database, you need to have alredy
moved the data into the global variables $Binding, and $Category before
you call SelectField(). If you are re-displaying a form with an error in
it, PHP will have already copied the values from the previous form, so
it will retain values set in the previous page. That makes it easy for
your users when the program finds an error.
Clear as mud, right? What it means is if this page calls itself as in
<FORM Action="<?=$PHP_SELF?>" Method=Post>
and you do nothing but paint the form, and hit the SUBMIT button, the
select fields will retain the user selected value by themselves.
Rick Widmer
Internet Marketing Specialists
www.developersdesk.com