My jquery code is below, This is a currency converting through a API.In the API datas in json. I want to retrieve the data through AJax.
Im geting the error : Fatal error: Cannot access empty property in C:\xampp\htdocs\mvc\converter\ajax.php on line 25
<script type="text/javascript">
$(document).ready(function(){
$("#currency").change(function(){
var currency= $("#currency").val();
$.ajax({
type: "GET",
url: "ajax.php",
cache: false,
data: currency,
dataType: "text",
success: function(data){
alert(data);
$("#quantity").keyup(function(){
//var local_rate= $("#local_rate").val();
var quantity= $("#quantity").val();
var us_rate= quantity / cur;
$('.listprice').html(us_rate);
});
}
});
});
});
</script>
here is my php (ajax.php) code
<?php
$currency =$_GET['currency'];
$file = 'latest.json';
$appId = '306bdd0f71fe465280e48188846534af';
// Open CURL session:
$ch = curl_init("http://openexchangerates.org/api/{$file}?app_id={$appId}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get the data:
$json = curl_exec($ch);
curl_close($ch);
$exchangeRates = json_decode($json);
echo $rate= $exchangeRates->rates->$currency;
?>
Im stuck on retrieving the data, without using jquery its working perfectly… In here I want to load through Ajax..
Change
data: currency,todata: {currency: currency},so the currency will be available via$_GET['currency']