Jump to content

Make Option Selected


smerny
 Share

Recommended Posts

I am making a form that includes a select/option list for country.For Gender I tried this:

	  <select name='gender'>		<option value='0' ".($gender == 0 ? "selected" : "").">Select...		<option value='1' ".($gender == 1 ? "selected" : "").">Male		<option value='2' ".($gender == 2 ? "selected" : "").">Female	  </select>

where $_POST['gender'] would be given to $gender above... so that if the form is filled out but has errors, they dont need to re-enter all the info.but i'm not sure if thats working either?anyway that would obviously take a long time to do for country... is there a good way to do this?

Edited by smerny
Link to comment
Share on other sites

Try this:

<?phpfunction gen($num){global $gender; //I think that how you get outter vars into a functionif($gender == $num){return echo "selected='selected'";}}?>	  <select name='gender'>		<option value='0' <? gen(0); ?>>Select...		<option value='1' <? gen(1); ?>>Male		<option value='2' <? gen(2); ?>>Female	  </select>

Link to comment
Share on other sites

You don't return echo. You either echo, or return. The return value of echo is null.If you have a big select list it's generally best to store your options and values in an array, and loop through the array to print the options. Inside the loop you can check if the current value is selected and print the appropriate attribute for it.

Link to comment
Share on other sites

<select name=country> <option value="Algeria" />Algeria <option value="Afghanistan" />Afghanistan <option value="Albania" />Albania <option value="Algeria" />Algeria <option value="American Samoa" />American Samoa <option value="Andorra" />Andorra <option value="Angola" />Angola .............etc

MANY many countries... what is an easy way to select the current value?

Link to comment
Share on other sites

Like I said, put them in a PHP array and loop through to print them out, you can check each one before you print it. You just need to put all the countries in an array. Here's a simple list to get you started if you want, you can add or remove countries if you want to, or change the order or whatever. You can save this in a text file and read it in using file_get_contents, and use explode to put each line in an array element, and then you have your array of countries.

United States of AmericaCanadaUnited KingdomAustraliaIndiaChinaAfghanistanAlbaniaAlgeriaAmerican SamoaAndorraAngolaAnguillaAntigua & BarbudaArgentinaArmeniaArubaAustriaAzerbaijanBahamasBahrainBangladeshBarbadosBelarusBelgiumBelizeBeninBermudaBhutanBoliviaBosnia & HerzegovinaBotswanaBouvet Is.BrazilBritish Indian Ocean Terr.British Virgin Is.Brunei DarussalamBulgariaBurkina FasoBurundiCambodiaCameroonCape VerdeCayman IslandsCentral African RepublicChadChileChristmas Is.Cocos (Keeling) Is.ColombiaComorosCongoCook Is.Costa RicaCóte D'ivoire (Ivory Coast)CroatiaCubaCyprusCzech RepublicDenmarkDjiboutiDominicaDominican RepublicEast TimorEcuadorEgyptEl SalvadorEquatorial GuineaEritreaEstoniaEthiopiaFalkland Is. (Malvinas)Faroe Is.FijiFinlandFranceFrench GuianaFrench PolynesiaFrench Southern TerritoriesGabonGambiaGeorgiaGermanyGhanaGibraltarGreeceGreenlandGrenadaGuadeloupeGuamGuatemalaGuineaGuinea-BissauGuyanaHaitiHeard & McDonald Is.HondurasHong KongHungaryIcelandIndonesiaIraqIrelandIslamic Republic of IranIsraelItalyJamaicaJapanJordanKazakhstanKenyaKiribatiKorea, North (DPRK)Korea, South (Rep. of)KuwaitKyrgyzstanLao People's Democratic Rep.LatviaLebanonLesothoLiberiaLibyan Arab JamahiriyaLiechtensteinLithuaniaLuxembourgMacauMadagascarMalawiMalaysiaMaldivesMaliMaltaMarshall Is.MartiniqueMauritaniaMauritiusMayotteMexicoMicronesiaMoldova, Republic ofMonacoMongoliaMonserratMoroccoMozambiqueMyanmarNamibiaNauruNepalNetherlandsNetherlands AntillesNew CaledoniaNew ZealandNicaraguaNigerNigeriaNiueNorfolk Is.Northern Mariana Is.NorwayOmanPakistanPalauPanamaPapua New GuineaParaguayPeruPhilippinesPitcairnPolandPortugalPuerto RicoQatarRéunionRomaniaRussian FederationRwandaS. Georgia & the S. Sandwich Is.SamoaSan MarinoSao Tome & PrincipeSaudi ArabiaSenegalSeychellesSierra LeoneSingaporeSlovakiaSloveniaSolomon Is.SomaliaSouth AfricaSpainSri LankaSt. HelenaSt. Kitts & NevisSt. LuciaSt. Pierre & MiquelonSt. Vincent & the GrenadinesSudanSurinameSvalbard & Jan Mayen Is.SwazilandSwedenSwitzerlandSyrian Arab RepublicTaiwan, Province of ChinaTajikistanTanzania, United Rep. ofThailandTogoTokelauTongaTrinidad & TobagoTunisiaTurkeyTurkmenistanTurks & Caicos Is.TuvaluUgandaUkraineUnited Arab EmiratesU.S. Virgin Is.U.S. Minor Outlying Is.UruguayUzbekistanVanuatuVenezuelaViet NamWallis & Futuna Is.Western SaharaYemenYugoslaviaZaireZambiaZimbabwe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...