Re: Forms, Option Select handling a choice?

From: Date: Thu, 11 Oct 2001 20:02:41 +0000
Subject: Re: Forms, Option Select handling a choice?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70851@lists.php.net to get a copy of this message
I use this function. Put it at the bottom of your program or in an include file somewhere: ################################ function arr2sel($arr,$sel = "") { // takes key/val pairs in $arr and outputs <option> tags for <select> statements while ( list( $key, $val ) = each($arr)) {
      if(isset($sel) && ($key == $sel || (is_array($sel) && in_array($key,$sel)))) {
         $select_v=" selected";
      } else {
         $select_v="";
      }
      echo "<option value=\"$key\" $select_v>$val</option>\n";
} } Define an array for the select statement like so:
    $YesNo=array(yes=>Yes,no=>No);
Then write the select like so: <select name="ot">
       <?php arr2sel($YesNo,$ot); ?>
</select> The big advantage of doing it this way is it makes the select "sticky". Marcus James Christian wrote:
How do you handle a choice from a users <option> within select of a form?


« previous php.general (#70851) next »