NYCPHP Meetup

NYPHP.org

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

Alan T. Miller amiller at hollywood101.com
Wed Jan 29 17:09:21 EST 2003


The following is a piece of code that demonstrates one way to do what you
are trying to do.

<?php

$arr = array (
    '0' => 'Please Select',
    'Weekly Rental' =>  'Weekly Rental',
    'Monthly Rental' => 'Monthly Rental',
    'Daily Rental' =>   'Daily Rental',
    'Annual Rental' =>  'Annual Rental',
    'Year Round Living' => 'Year Round Living',
    'other' => 'Other'
    );

    echo"<select name=\\"looking_for\\" class=\\"smalltext\\">\
";
    foreach($arr as $key=>$val) {
        if($looking_for == $key) {
            echo"\	<option value=\\"$key\\" SELECTED>$val</option>\
";
        } else {
            echo"\	<option value=\\"$key\\">$val</option>\
";
 }
    }
    echo"</select>\
";
?>




----- Original Message -----
From: "Hans Zaunere" <zaunere at yahoo.com>
To: "NYPHP Talk" <talk at nyphp.org>
Sent: Wednesday, January 29, 2003 1:38 PM
Subject: Re: [nycphp-talk] Remembering which option in a SELECT the user
picked.


>
> --- Webapprentice <webapprentice at onemain.com> 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>
>
> Just my opinion, but to reduce processing and possible error, you may want
to
> consider making the value= some sort of shorter representation of the
value
> you're after.  This is really pedantic, though.
>
> > 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.
>
> Same here, so I wrote a silly little function:
>
>
> function is_selected( $val,$val1 ) {
>    if( $val == $val1 )
>       return " SELECTED value=\\"$val1\\" ";
>    else
>       return " value=\\"$val1\\" ";
> }
>
> which I use along these lines:
>
> <select name=respon>
>   <option <?=is_selected($_POST['respon'],'one')?> One! </option>
>   <option <?=is_selected($_POST['respon'],'two'> Two! </option>
>   <option <?=is_selected($_POST['respon'],'three')?> Three! </option>
>   <option <?=is_selected($_POST['respon'],'four')?> Four! </option>
> </select>
>
>
>
> =====
> Hans Zaunere
> President, New York PHP
> http://nyphp.org
> hans at nyphp.org
>
>
> --- Unsubscribe at http://nyphp.org/list/ ---
>
>
>




More information about the talk mailing list