php中的货币转换器:只有一方转换有效

I am trying to implement currency convertion in my web page.

Here is the work flow

1.User can select the currency from drop down menu.

2.According to this prizes listed in the page is changed with respect to newly selected currency.

Code

1.When a user select an item from dropdown menu currency value set in a session variable and the page is reloaded using ajax call .

<select id="curr-dropmenu">
    <option disabled="" selected="">Choose</option>
<option value="INR">INR</option>
<option value="USD">USD</option>
<option value="AED">AED</option>
<option value="FED">FED</option>
<option value="XBJ">XBJ</option>
</select>

ajax call

$('#curr-dropmenu').on('change',function(e){
       //ajax call for setting the currency in session
       $.ajax({
           url:'ajax_set.php',
           data:{currency:$(this).val()},
           method:"POST",
           success:function(data){

               location.reload();
           },
           error:function(data){

               alert("ERROR"+JSON.strinfy(data));
           }
       });
    });

In ajax_set.php file

<?php
session_start();  
$_SESSION['old_currency']= (!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
$_SESSION['new_currency']=$_POST['currency'];

2.When the page is reloaded get the previous and current currency are fetched from the session and make an api request to google currency converter to convert the amount to the selected prize.

    <?php
     //getting the currency values from session.
         session_start();
       //old currency
         $from=(!empty($_SESSION['old_currency']))?$_SESSION['old_currency']:'INR';
         echo 'From:&nbsp;'.$from.'<br>';
  //new currency  
  $to=(!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
         echo 'To:&nbsp;'.$to.'<br>';
    //amount to be converted.

    $prize=array('2000','600','7000','2500','100000');
    echo '<br><br>';
    for($i=0;$i<5;$i++){
        //call the currency convert function 
        if($from!=$to)
        {
            $response=currencyConvert($from,$to,$prize[$i]);
            $converted_amount=filter_var($response,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
        }
        else
        {
            $converted_amount=$prize[$i];
        }
        echo '<span>'.$converted_amount.' '.$to.'</span><hr/>';
    }


//make the api request for convert currency.
function currencyConvert($from,$to,$amount){
  // echo '<br>convert'.$f.'to'.$t;
    $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 

    $request = curl_init(); 
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request);
    curl_close($request);
    $regex  = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regex, $response, $converted);
    if(!empty($converted))
    return  ($converted[0])?$converted[0]:0;

}

This will works only in one way eg: if i convert INR to USD the conversion is correct,but when i select USD to INR it will get the wrong output.

Hi can it be that you send the same currency to the google service?

Change your check from

if ($from == $to)

to

if (strtoupper($from) == strtoupper($to))

HI you need to change in php script to send converted prizes to google converter try the following code and check

     //getting the currency values from session.
         session_start();
       //old currency
         $from=(!empty($_SESSION['old_currency']))?$_SESSION['old_currency']:'INR';
         echo 'From:&nbsp;'.$from.'<br>';
  //new currency  
  $to=(!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
         echo 'To:&nbsp;'.$to.'<br>';
    //amount to be converted.

    $prize=array('2000','600','7000','2500','100000');
 $prize_old =(!empty($_SESSION['old_prize']))?$_SESSION['old_prize']:$prize; 
$prize_new =(!empty($_SESSION['new_prize']))?$_SESSION['new_prize']:$prize;  
$prize_converted = array();
    echo '<br><br>';
    for($i=0;$i<5;$i++){
        //call the currency convert function 
        if($from!=$to)
        {
            $response=currencyConvert($from,$to, $prize_new[$i]);
            $converted_amount=filter_var($response,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
            array_push($prize_converted,round($converted_amount));
        }
        else
        {
            $converted_amount=$prize[$i];
             array_push($prize_converted,$converted_amount);

        }
        echo '<span>'.$converted_amount.' '.$to.'</span><hr/>';
    }
$_SESSION['old_priz'] = $prize;  
$_SESSION['new_prize'] = $prize_converted;


//make the api request for convert currency.
function currencyConvert($from,$to,$amount){
  // echo '<br>convert'.$f.'to'.$t;
     $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 

    $request = curl_init(); 
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request);
    curl_close($request);
    $regex  = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regex, $response, $converted);
    if(!empty($converted))
    return  ($converted[0])?$converted[0]:0;

}

I hope this help