从下拉PHP输出正确的值(数组)

时间:2019-03-04 21:42:21

标签: php html

我试图找到一种方法来从保管箱中打印正确的值,但我无法设法使其正常工作。我希望下拉框写成这样:如果我选择了比特币,“ BTC”的最后一个已知货币是:“ 17”。非常感谢您的帮助!

    <?php
$v2 = array(
    "BTC" => array(
        "navn" => "Bitcoin",
        "k1" => "1",
        "k2" => "11",
        "k3" => "17"
    ),
    "ETH" => array(
        "navn" => "Ethereum",
        "k1" => "2",
        "k2" => "12",
        "k3" => "27"
    ),
    "LTC" => array(
        "navn" => "Litecoin",
        "k1" => "3",
        "k2" => "13",
        "k3" => "37"
    ),
    "XMR" => array(
        "navn" => "Monero",
        "k1" => "4",
        "k2" => "14",
        "k3" => "47"
    ),
    "XRP" => array(
        "navn" => "Ripple",
        "k1" => "5",
        "k2" => "15",
        "k3" => "57"
    )
);
echo "<form action = 'valgtvaluta.php' method='post'><select name=''style='width: 200;'> >";
foreach($v2 as $key => $value) {
    echo "<option value=".$key.">".$key." ".$value['navn']."</option>";
}
echo "<input type='Submit'></select></form>";

$v2[$_POST['select']]['k3']

?>

1 个答案:

答案 0 :(得分:0)

您需要给<select>命名,使其与您在$_POST中寻找的名称相匹配。

<input type="submit">不应位于<select>内。

然后检查表单是否已提交,并回显您想要的内容。

<?php
$v2 = array(
    "BTC" => array(
        "navn" => "Bitcoin",
        "k1" => "1",
        "k2" => "11",
        "k3" => "17"
    ),
    "ETH" => array(
        "navn" => "Ethereum",
        "k1" => "2",
        "k2" => "12",
        "k3" => "27"
    ),
    "LTC" => array(
        "navn" => "Litecoin",
        "k1" => "3",
        "k2" => "13",
        "k3" => "37"
    ),
    "XMR" => array(
        "navn" => "Monero",
        "k1" => "4",
        "k2" => "14",
        "k3" => "47"
    ),
    "XRP" => array(
        "navn" => "Ripple",
        "k1" => "5",
        "k2" => "15",
        "k3" => "57"
    )
);
echo "<form action = 'valgtvaluta.php' method='post'>
        <select name='select' style='width: 200;'>";
foreach($v2 as $key => $value) {
    echo "<option value=".$key.">".$key." ".$value['navn']."</option>";
}
echo "</select>
    <input type='Submit' name='submitbtn'>
    </form>";

if (isset($_POST['submitbtn'])) {
    echo "The last known value for {$_POST['select']} is {$v2[$_POST['select']]['k3']}";
}

?>