[thelist] Returning form values of inspection

Rich Gray rich at f1central.net
Sat Feb 8 22:35:01 CST 2003


> I did not realize before that SELECT types of form fields would not
> easily return the selected value through a NAME variable like a simple
> TEXT type does. For instance
>
> <INPUT TYPE="text" NAME"firstname" VALUE='<?php echo "$firstname";?>'>
>
> Will return the value assigned to firstname when the form is returned
> subsequent times. I'm having trouble getting a selection to do the
> same. perhaps it cannot. For instance
>
> <P><STRONG>Binding Type:</STRONG><BR>
> 					<SELECT NAME="binding_type"
>  SIZE=1 MAXLENGTH=50>
> 					<OPTION
> VALUE="perfect_binding">Perfect Binding</OPTION>
> 					<OPTION VALUE="stapled"
> >Stapled</OPTION>
> 					<OPTION VALUE="hard_cover"
> >Hard Cover</OPTION>
> 					</SELECT>
>
> I don't think SELECT  allows a VALUE to be assigned so when a form is
> re-entered the default is returned instead of the previously selected
> value. How does one get around this? In other words, I want to assign
> the selection to a variable that can be returned if the form is viewed
> again for verification.
>
> Mark Gillingham
> markgill at uwalumni.com
> www.loftnet.com/roots/
> -
Mark

You need to get your script to output the SELECTED attribute on the OPTION
tag for the previously selected option ... here is a very basic PHP function
that will do that based on a passed array and previously selected value...

function do_select($list,$sel=false) {
    echo '<select>'."\n";
    foreach ($list as $key => $val) {
        echo '<option value="'.$key.'"'.($key == $sel ? ' selected' :
'').'>'.$val.'</option>'."\n";
    }
    echo '</select>';
}

HTH
Rich




More information about the thelist mailing list