RE: [PHP] automatically selecting HTML drop down

From: Date: Tue, 15 Aug 2000 10:03:06 +0000
Subject: RE: [PHP] automatically selecting HTML drop down
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11765@lists.php.net to get a copy of this message
There are several ways you can do this. I prefer to use an include file where I list all states in array then I use a function to call out the states and check to see if any of them should be selected. This I think is a cleaner way of doing it. If you are interested in this method let me know. Here is another way of doing it. Not as clean, but it will work. Assuming $state has been assigned from your Database query: <SELECT NAME="state"> <OPTION VALUE="Alabama" <? if($state == "Alabama"){ echo "SELECTED"; } ?>>Alabama</OPTION> <OPTION VALUE="Arizona" <? if($state == "Arizona"){ echo "SELECTED"; } ?>>Arizona</OPTION> <OPTION VALUE="California" <? if($state == "California"){ echo "SELECTED"; } ?>>California</OPTION> etc... etc.... </SELECT> As I said, this is not the cleanest way to do it since you will have to do this for every single state. vs just calling out a function on your page that looks like this: stateSelectMenu(state, $state); Just to give you a heads up, this is what the include file with the array and funtion method would look like. $States = array( "Alabama"=>"Alabama", "Arizona"=>"Arizona", "California"=>"California" ); function stateSelectMenu($name, $selected) { global $States; echo "<SELECT NAME=\"$name\" SIZE=\"1\">"; echo "<OPTION VALUE=\"\">- Select State -</OPTION>\"; for(reset($States); $key = key($States); next($States)) { echo "<OPTION VALUE=\"$key\""; if($key == $selected) { echo " SELECTED"; } echo ">$StatesProvince[$key]</OPTION>"; } echo "</SELECT>"; } -----Original Message----- From: Dustin Leggans [mailto:dustinl.rm@yahoo.com] Sent: Sunday, August 13, 2000 10:54 PM To: php-general@lists.php.net Subject: [PHP] automatically selecting HTML drop down Hello.. I am creating a phone book database... I have an "edit" page where users can come in and change their information, certain fields, like STATE, have concrete values that will not vary from user to user therefore I use a drop down list. When this edit page comes up, I would like the STATE dropdown to default to the STATE value that the user has. For example, if a user from California logs in to change some information, the drop-down state list should default to the CA value. Any ideas? Thanks, dustin _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#11765) next »