<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Convert extends CI_Controller {
public function __construct()
{
parent::__construct();
/* if ($this- >session- >userdata('logged_in') != 1){
redirect('home');
} */
}
public function index(){
$this- >load- >view('index.php');
}
public function convert(){
function currencyConverter($fromCurrency,$toCurrency,$amount) {
$fromCurrency = urlencode($fromCurrency);
$toCurrency = urlencode($toCurrency);
$encode_amount = 1;
$url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;
$get = file_get_contents($url);
$data = preg_split('/\D\s(.*?)\s=\s/',$get);
$exhangeRate = (float) substr($data[1],0,7);
$convertedAmount = $amount*$exhangeRate;
$data = array( 'exhangeRate' = > $exhangeRate, 'convertedAmount' = >$convertedAmount, 'fromCurrency' = > strtoupper($fromCurrency), 'toCurrency' = > strtoupper($toCurrency));
echo json_encode( $data );
}
if(1==1) {
$from_currency = trim($_POST['from_currency']);
$to_currency = trim($_POST['to_currency']);
$amount = trim($_POST['amount']);
if($from_currency == $to_currency) {
$data = array('error' = > '1');
echo json_encode( $data );
exit;
}
$converted_currency=currencyConverter($from_currency, $to_currency, $amount);
echo $converted_currency;
}
}
<!DOCTYPE html >
<html >
<head >
<title >Current Weather </title >
</head>
<body>
<div class="card">
<div class="card-body">
<div class="text-dark font-size-18 font-weight-bold mb-1" >Currency XE </div>
<form method="post" id="currency-form">
<div class="form-group" >
<label >From </label>
<select name="from_currency" class="form-control">
<option value="INR" >Indian Rupee </option>
<option value="USD" selected="1" >US Dollar </option>
<option value="AUD" >Australian Dollar </option>
<option value="EUR" >Euro </option >
<option value="EGP" >Egyptian Pound </option>
<option value="CNY" >Chinese Yuan </option>
</select >
<label >Amount </label>
<input type="text" placeholder="Currency" name="amount" class="form-control" id="amount" />
<label >To </label >
<select name="to_currency" class="form-control">
<option value="INR" selected="1" >Indian Rupee </option>
<option value="USD" >US Dollar </option>
<option value="AUD" >Australian Dollar </option>
<option value="EUR" >Euro </option >
<option value="EGP" >Egyptian Pound </option>
<option value="CNY" >Chinese Yuan </option>
</select>
<br>
<button type="button" name="convert" id="convert" class="btn btn-default" >Convert</button>
</div>
</form>
<div class="form-group" id="converted_rate" > </div>
<div id="converted_amount"> </div>
</div>
</div>
$('document').ready(function() {
/* Handling Currency Convert functionality */
$('#convert').on('click', function() {
var data = $("#currency-form").serialize();
$.ajax({
type : 'POST',
url : 'http://localhost/trading_new/Dashboard/convert',
dataType:'json',
data : data,
beforeSend: function(){
$("#convert").html(' <span class="glyphicon glyphicon-transfer" > </span > converting ...');
},
success : function(response){
//alert(response);
if(response.error == 1){
$("#converted_rate").html(' <span class="form-group has-error" >Error: Please select different currency </span >');
$("#converted_amount").html("");
$("#convert").html('Convert');
$("#converted_rate").show();
} else if(response.exhangeRate){
$("#converted_rate").html(" <strong >Exchange Rate ("+response.toCurrency+" </strong >) : "+response.exhangeRate);
$("#converted_rate").show();
$("#converted_amount").html(" <strong >Converted Amount ("+response.toCurrency+" </strong >) : "+response.convertedAmount);
$("#converted_amount").show();
$("#convert").html('Convert');
} else {
$("#converted_rate").html("No Result");
$("#converted_rate").show();
$("#converted_amount").html("");
}
}
});
});
});
</body >
</html >