John Laimuin
John Laimuin

Reputation: 91

issue with simple currency conversion with php

I cant get the value of the selected currency to be converted properly. Did I correctly set up my 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 I comment his I get an undefined index at the line below

    $amount_output = $amount_input*$currencies[$currency1][$currency2];

    ?>

Form to convert the currency to the 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 are not being input converted here?

      <input type="submit" name="submit" value="Convert"></input>

        </form> 

May also get undefined index error if the $currencies = 0; is comment

Upvotes: 1

Views: 131

Answers (3)

innerpeace
innerpeace

Reputation: 112

the default option in you select type is shown below:

currency_type_from  EUR
currency_type_to    EUR

but array keys 'EUR' does not exist in $currencies, so you get an undefined index error

Upvotes: 0

Nawin
Nawin

Reputation: 1692

Add it to string inside the array index. Make sure your array key is USD or USB:

$amount_output = $amount_input*$currencies["$currency1"]["$currency2"];

For the testing code is You can test HERE this full php code:

    $amount_input = 4;
    $currency1 = 'GBP'; //from
    $currency2 = 'USD'; //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['USD'] = array(
        'USD' => 1,
        'EUR' => 0.849851,
        'GBP' => 0.774699
    );

    //$currencies = 0; // when I comment his I get an undefined index at the line below

    $amount_output = $amount_input*$currencies["$currency1"]["$currency2"];
    echo '<pre>';
print_r($amount_output);

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

Do as following

1) If you want output on same page, then don't add action(of form) of second page. leave as empty.

2) Add php code before Form so you can get 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> 

Upvotes: 2

Related Questions