issue with simple currency conversion with php -
i cant value of selected currency converted properly. did correctly set array?
<?php $amount_input = filter_input(input_post, 'amount_input'); $currency1 = filter_input(input_post, 'currency_type_from'); $currency2 = filter_input(input_post, 'currency_type_to'); $currencies = array(); $currencies['gbp'] = array( 'gbp' => 1, 'eur' => 1.09710, 'usd' => 1.28917 ); $currencies['eur'] = array( 'eur' => 1, 'gbp' => 0.911383, 'usd' => 1.17616 ); $currencies['usb'] = array( 'usd' => 1, 'eur' => 0.849851, 'gbp' => 0.774699 ); $currencies = 0; // when comment undefined index @ line below $amount_output = $amount_input*$currencies[$currency1][$currency2]; ?>
form convert currency select type
<form action="converter.php" method="post"> <select name="currency_type_from" class="form-control"> <option value="eur">eur</option> <option value="gbp">gbp</option> <option value="usd">usd</option> </select> <input name="amount_input" type="text"/> <select name="currency_type_to" class="form-control"> <option value="eur">eur</option> <option value="gbp">gbp</option> <option value="usd">usd</option> </select> <input name="amount_output" type="text" value="<?php echo $amount_output; ?>" /> // value not being input converted here? <input type="submit" name="submit" value="convert"></input> </form>
may undefined index error if $currencies = 0; comment
do following
1) if want output on same page, don't add action(of form) of second page. leave empty.
2) add php code before form can convered value
<?php $amount_output = ""; $amount_input = ""; if($_post) { $amount_input = filter_input(input_post, 'amount_input'); $currency1 = filter_input(input_post, 'currency_type_from'); $currency2 = filter_input(input_post, 'currency_type_to'); $currencies = array(); $currencies['gbp'] = array( 'gbp' => 1, 'eur' => 1.09710, 'usd' => 1.28917 ); $currencies['eur'] = array( 'eur' => 1, 'gbp' => 0.911383, 'usd' => 1.17616 ); $currencies['usb'] = array( 'usd' => 1, 'eur' => 0.849851, 'gbp' => 0.774699 ); $amount_output = $amount_input*$currencies[$currency1][$currency2]; } ?> <form action="" method="post"> <select name="currency_type_from" class="form-control"> <option value="eur">eur</option> <option value="gbp">gbp</option> <option value="usd">usd</option> </select> <input name="amount_input" type="text" value="<?php echo $amount_input; ?>"/> <select name="currency_type_to" class="form-control"> <option value="eur">eur</option> <option value="gbp">gbp</option> <option value="usd">usd</option> </select> <input name="amount_output" type="text" value="<?php echo $amount_output; ?>" /> <input type="submit" name="submit" value="convert"></input> </form>
Comments
Post a Comment