Re: php/mySQL and html <select>

From: Date: Sun, 06 May 2001 20:04:10 +0000
Subject: Re: php/mySQL and html <select>
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-9138@lists.php.net to get a copy of this message
Hello Nick, > hi... > > I have a user registration system that I need to add a > "state" field to. Part of the form will look something > like this but with all 50 states instead of just two: > > <form> > <select name="state"> > <option value="AK">AK</option> > <option value="AL">AL</option> > </select> > </form> > > ...how do I insert the user's state into the database > and then how do I show the user's state as the > "selected" state in the drop down list later when the > user comes back to update his/her profile? > > hope this makes sense.. thanks, > > nick you have to build the <option> tags in PHP such as: $states = array("AK" => "AK", "AL" =>"AL", ...); while (list($key, $state) = each($states)) // iterates over $states array { printf("<option value='%s' %s>%s</option>", $key, ($key == $user_state?'selected':''), $state); //user_state equals to the key of state currently printed out then append 'selected' attribute to the option tag }

« previous php.db (#9138) next »