为了账号安全,请及时绑定邮箱和手机立即绑定

如何输出值,从选定的选项作为关联数组的键

如何输出值,从选定的选项作为关联数组的键

PHP
呼唤远方 2021-10-08 13:06:56
我正在处理如何从关联数组输出选定的选项。我使用 foreach 循环来获取选项列表中的所有数组键,但我需要值来计算输出率。我应该如何显示我目前拥有的键,但要计算速率我需要键。提前感谢南希的任何帮助<?php$rate = ['Euro' => 25.62, 'USD' => 22.74, 'GBP' => 28.50, 'AUS' => 15.90, 'CHN' => 3.30];?>html:        <form action="" method='POST'> <input type="number" name='from'  step=any  min='1' placeholder='How many euros are you exchanging?' required>  <select name="rate"><?phpforeach($rateFrom as $key => $value):echo '<option type= "number" name='. $value.'>'. $key.'</option>'; endforeach;?></select>          <input type="submit" name='submit'></form><?phpif(isset($_POST['submit'])){$rate = $_POST['rate'];$from = $_POST['from'];$to = $from * $rate;}?>
查看完整描述

1 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

在代码中添加了带有注释的详细信息。


<?php

    $rateFrom = ['Euro' => 25.62, 'USD' => 22.74, 'GBP' => 28.50, 'AUS' => 15.90, 'CHN' => 3.30];

    ?>



     <form action="" method='POST'>

     <input type="number" name='from'  step=any  min='1' placeholder='How many euros are you exchanging?' required>  

    <!-- create a input hidden field call currency_name-->

     <input type="hidden" id="currency_name" name="currency_name">


     <!-- create a input hidden field. on change the value, get the select option currency name and add into 'currency_name' field.-->

    <select name="rate" onChange="document.getElementById('currency_name').value = this.options[this.options.selectedIndex].text;">

    <?php

    foreach($rateFrom as $key => $value):

    //use  value attr

    echo '<option value='. $value.'>'. $key.'</option>'; 

    endforeach;

    ?>

    </select>          

    <input type="submit" name='submit'>

    </form>


    <?php

    if(isset($_POST['submit'])){


    $rate = $_POST['rate'];

    $from = $_POST['from'];


    //get the currency name by key 'currency_name'

    $curency = $_POST['currency_name'];

    echo 'Your amount'.$from . ' ' .$curency;

    $to = $from * $rate;

    }

    ?>


查看完整回答
反对 回复 2021-10-08
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信