NYCPHP Meetup

NYPHP.org

[nycphp-talk] Remembering which option in a SELECT the user picked.

gadhra bfeqx44002 at sneakemail.com
Wed Jan 29 00:14:39 EST 2003


Hi Stephen,

You might consider offloading the creating of the select to a function call:


function generate_select($name,$option,$sel=0) {
      $str = "\
<select name=\\"$name\\">\
";
      $str .= "<option value=\\"0\\">- None -</option>\
";
      foreach ($option as $key=>$val) {
        if ($sel == $val) {
           $str .="<option value=\\"$val\\" selected>$key</option>\
";
       } else {
          $str .= "<option value=\\"$val\\">$key</option>\
";
      }
    }
     $str .= "</select>\
";
     return $str;
  }

Then, you can just create an array of the values you want in select:
/*  I used a numerical representation of the date time to avoid 
confusion between the key and value */
$arr_times = array(    "Friday August 22, 04:00 PM - 06:00 PM"=> 
"082203_1600_1800",    "Friday August 22, 06:00 PM - 08:00 PM"=> 
"082203_1800_2000");

and run the function like this:

$display_select = generate_select("TimeChoice",$arr_times);
if ($error) {
  $display_select = 
generate_select("TimeChoice",$arr_times,$_POST['TimeChoice']);
}

If you get an error, you will retain the HTTP_POST_VARS for your select 
value, and you should be in business.  Bear in mind I haven't tested 
this out, but I think it will work; anyone see problems with this?

+gadhra+


Webapprentice webapprentice-at-onemain.com |NY PHP| wrote:

>Hello NYPHP group,
>I'm searching for an answer to this, that I don't find in the usual 
>resources (i.e. PHP cookbook, www.php.net).  My PHP skill is novice at 
>best, so I could use some help.
>
>
>I have a SELECT on a web form.
>
><select name="TimeChoice">
><option value="Friday August 22, 04:00 PM - 06:00 PM">Friday August 22, 
>04:00 PM - 06:00 PM</option>
><option value="Friday August 22, 06:00 PM - 08:00 PM">Friday August 22, 
>06:00 PM - 08:00 PM</option>
><option value="Friday August 22, 08:00 PM - 10:00 PM">Friday August 22, 
>08:00 PM - 10:00 PM</option>
><option value="Friday August 22, 10:00 PM - 12:00 AM">Friday August 22, 
>10:00 PM - 12:00 AM</option>
><option value="Saturday August 23, 12:00 AM - 02:00 AM">Saturday August 
>23, 12:00 AM - 02:00 AM</option>
><option value="Saturday August 23, 12:00 PM - 02:00 PM">Saturday August 
>23, 12:00 PM - 02:00 PM</option>
><option value="Saturday August 23, 02:00 PM - 04:00 PM">Saturday August 
>23, 02:00 PM - 04:00 PM</option>
><option value="Saturday August 23, 04:00 PM - 06:00 PM">Saturday August 
>23, 04:00 PM - 06:00 PM</option>
><option value="Saturday August 23, 06:00 PM - 08:00 PM">Saturday August 
>23, 06:00 PM - 08:00 PM</option>
><option value="Saturday August 23, 08:00 PM - 10:00 PM">Saturday August 
>23, 08:00 PM - 10:00 PM</option>
><option value="Saturday August 23, 10:00 PM - 12:00 AM">Saturday August 
>23, 10:00 PM - 12:00 AM</option>
><option value="Sunday August 24, 12:00 AM - 02:00 AM">Sunday August 24, 
>12:00 AM - 02:00 AM</option>
><option value="Sunday August 24, 12:00 PM - 02:00 PM">Sunday August 24, 
>12:00 PM - 02:00 PM</option>
><option value="Sunday August 24, 02:00 PM - 04:00 PM">Sunday August 24, 
>02:00 PM - 04:00 PM</option>
></select>
>
>Only one option may be chosen.
>
>When I submit this form, there could be errors.  When I have errors, I 
>wish to reprint the form with the user's values remembered.  In the 
>reprinted SELECT tag, one of the OPTION tags will now have a SELECTED 
>attribute.  Is there any elegant way to insert the SELECTED attribute on 
>the right option?
>
>I've been running into this situation a lot.
>
>Thank you for your time and help.
>
>--Stephen
>
>
>
>--- Unsubscribe at http://nyphp.org/list/ ---
>
>  
>




More information about the talk mailing list