# Converting the entered currency amounts when I change the currency

**URL:** <https://www.phphelp.com/t/converting-the-entered-currency-amounts-when-i-change-the-currency/34912>\
**Category:** Javascript & Ajax\
**Created:** [August 2, 2022, 8:45am UTC](https://www.phphelp.com/t/converting-the-entered-currency-amounts-when-i-change-the-currency/34912 "2022-08-02T08:45:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 2, 2022, 8:45am UTC](https://www.phphelp.com/t/converting-the-entered-currency-amounts-when-i-change-the-currency/34912/1 "2022-08-02T08:45:53Z")

</div>

Hello,  
When I change the currency with the Select option option, I want the coins just below to be converted

 ![Ekran görüntüsü 2022-08-02 112751](https://www.phphelp.com/uploads/default/original/2X/a/aa555a0c138326f22ae9aba47ef5c7c2bc9f5cd8.png)

```auto
<select size="1" name="currency">
<option selected value="USD">USD</option>
<option value="EURO">EURO</option>
<option value="TRY">TRY</option>
</select>

<input type="text" name="" value="320.50">
<input type="text" name="" value="150.00">
<input type="text" name="" value="250.35">

```

Exchange rates available with php

```auto
$usd = 17.9846; //TRY  
$euro = 18.4251; //TRY  
$euro_usd = 1.0245; //USD

```

I want it to convert without refreshing the page  
Page is not refreshed  
USD to TRY,  
TRY to EURO,  
USD to EURO,  
EUR to TRY  
as  
I will be glad if you help

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 2, 2022, 3:36pm UTC](https://www.phphelp.com/t/converting-the-entered-currency-amounts-when-i-change-the-currency/34912/2 "2022-08-02T15:36:56Z")

</div>

> <https://stackoverflow.com/questions/48679184/how-to-get-the-previous-selected-value-from-a-dropdown-before-onchange>

I adapted the code I found above for myself and got results.  
I don’t know how right I did

```auto
$('#secilen_parabirimi').change(function(){
var oldvar = $(this).attr('data-old') !== typeof undefined? $(this).attr('data-old') :"";
var newval = $("option:selected",this).text();
$(this).attr('data-old',newval)

var ucret = $("#ucret").val();

var usd = '17.9846';
var euro = '18.4251';
var usd_euro = '1.0245';

if(oldvar!==undefined){ // So that the page does not convert money while loading
  if(oldvar==='USD' && newval==='TRY'){
    $("#ucret").val((usd*ucret).toFixed(2));
  }
  if(oldvar==='USD' && newval==='EURO'){
    $("#ucret").val((ucret/usd_euro).toFixed(2));
  }
  if(oldvar==='EURO' && newval==='TRY'){
    $("#ucret").val((euro*ucret).toFixed(2));
  }
  if(oldvar==='EURO' && newval==='USD'){
    $("#ucret").val((ucret*usd_euro).toFixed(2));
  }
  if(oldvar==='TRY' && newval==='EURO'){
    $("#ucret").val((ucret/euro).toFixed(2));
  }
  if(oldvar==='TRY' && newval==='USD'){
    $("#ucret").val((ucret/usd).toFixed(2));
  }
}

}).change();

```

There is only one problem.  
When converting multiple times in a row, the result increases by odds due to rounding  
Sample:  
830.00 =\> 830.75, 830.01 as
