Web Development

Country Code Drop Down list

HTML CODE FOR DROP DOWN Menu

<select name=”county_list”>
<option value=”1″>USA</option>
<option value=”2″>UK</option>
<option value=”3″>Japan</option>
<option value=”4″ selected=”selected”>India</option>
<option value=”5″>Australia</option>
</select>
<input value=”” name=”name” type=”text”>
<input value=”go” name=”go” type=”submit”>

PHP CODE FOR DROP Down Menu With Selected value

<?php
$the_key = 4; // or whatever you want
$country = array( 1 => ‘USA’, 2 => ‘UK’, 3 => ‘Japan’, 4 => ‘India’, 5 => ‘Australia’);
?>

<select name=”county_list”>
<?php
foreach( $country  as $key => $val)
{
?><option value=”<?php echo $key; ?>”<?php
if($key==$the_key)echo ‘ selected=”selected”‘;
?>><?php echo $val; ?></option><?php
}
?>
</select>
<input type=”text” value=”” name=”name”>
<input type=”submit” value=”go” name=”go”>

Here you will get India as selected in Drop Down list because we use its key value of “4”. You can put value inside $the_key from Database if you are doing it during edit form of a user.

country_code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s