'Foreach loop shows the last key in select menu

i am making a crypto converter as a project for my school. I am using a api key for that and the array of the api is:

array(10) {
      ["ADA"]=>
      float(1.024301)
      ["BNB"]=>
      float(402.82399)
      ["BTC"]=>
      float(40678.195596)
      ["DOGE"]=>
      float(0.140886)
      ["ETH"]=>
      float(2877.712908)
      ["LTC"]=>
      float(119.283982)
      ["NEO"]=>
      float(26.72906)
      ["USDT"]=>
      float(1.008932)
      ["XMR"]=>
      float(163.338038)
      ["XRP"]=>
      float(0.780664)
}

And this the loop i am using to loop through array in a select menu:

<form method="post" action="">
            <input type="text" name="amount" placeholder="Enter Amount"><br><br>
            <select name="from">
            <?php
            Foreach ($arr_usd["rates"] as $currency => $rates) {
                print "<option value='$rates'>$currency</option>";
            }
            ?>
            </select>
            <select name="to">
                <option value="USD">US Dollar</option>
                <option value="EUR">Euro</option>
            </select>
            <br><br>
            <button class="btn" name="convert_crypto">Convert</button>
            <div id="result"><?php include "post.php" ?>
            </div>
        </form>

The select menu works as it should be, it displays the crypto names. The calculation of the rates works too.
But when i echo the answer, it echoes the last key of the currency in this case XRP.

if(isset($_POST['convert_crypto'])) {
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];
    if ($to == "USD") {
        $result = ($amount * round($from,2));
        echo $amount . " " . $currency . " = " . $result . " US Dollar";
    }    
}


Solution 1:[1]

You do not have any $currency reference. However, if - and that would make sense here, you have the part of code which prints option tags with the catching POST in the same file, in the same order (printer first, catcher then), then a $currency variable is loaded by the very last information it could obtain in the foreach loop.
Normally, you do not want that. I believe you would rather set a 'value' of each option as a currency, and only then, when the form is posted, you load a rate for the selected currency.

EDIT: Only parameters within "value" from a form is sent to backend. That means <option value=rate>currency</option> will provide only rate, NOT the currency

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1