使用选择框获取关联数组的值

时间:2014-10-17 03:12:24

标签: php html

我有一个显示关联数组键的选择框,我可以检测用户选择的选项。但我想知道的是我如何让PHP使用所选键的值。

$EU = array( "Italy"=>"Rome", "Luxembourg"=>"Luxembourg",
"Belgium"=> "Brussels", "Denmark"=>"Copenhagen", 
"Finland"=>"Helsinki", "France" => "Paris", 
"Slovakia"=>"Bratislava", "Slovenia"=>"Ljubljana", 
"Germany" => "Berlin", "Greece" => "Athens",
"Ireland"=>"Dublin", "Netherlands"=>"Amsterdam",
"Portugal"=>"Lisbon", "Spain"=>"Madrid", 
"Sweden"=>"Stockholm", "United Kingdom"=>"London", 
"Cyprus"=>"Nicosia", "Lithuania"=>"Vilnius", 
"Czech Republic"=>"Prague", "Estonia"=>"Tallin",
"Hungary"=>"Budapest", "Latvia"=>"Riga", "Malta"=>"Valetta", 
"Austria" => "Vienna", "Poland"=>"Warsaw");
ksort($EU);
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="country_select">
    <?php foreach ($EU as $country => $capital) { ?>
        <option value="<?php echo $country; ?>"><?php echo $country; ?></option>
    <?php
    }
    ?>
    </select>
    <input type="submit" name="submit" value="Confirm">
</form>
<?php
$country = $_POST['country_select'];
if (ISSET($_POST['submit'])) {
    echo "The capital city of your country, " . $country . ", is " . $capital . ".";
}
?>

输出显示所选国家,但它表示首都是伦敦。

0 个答案:

没有答案