我正在尝试在我的网页中实现货币转换。
这是的工作流程
1.用户可以从下拉菜单中选择货币。
2.根据此,页面中列出的奖品将根据新选择的货币进行更改。
代码
1.当用户从会话变量中设置的下拉菜单货币值中选择一个项目,并使用ajax
调用重新加载页面时。
<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调用
$('#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));
}
});
});
在ajax_set.php
文件中
<?php
session_start();
$_SESSION['old_currency']= (!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
$_SESSION['new_currency']=$_POST['currency'];
2.当页面被重新加载时,从会话中获取以前和当前的货币,并向google currency converter
发出api请求,将金额转换为所选奖金。
<?php
//getting the currency values from session.
session_start();
//old currency
$from=(!empty($_SESSION['old_currency']))?$_SESSION['old_currency']:'INR';
echo 'From: '.$from.'<br>';
//new currency
$to=(!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
echo 'To: '.$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;
}
这只能在一个方面起作用例如:如果我将印度卢比转换为美元,转换是正确的,但当我选择美元转换为印度卢比时,会得到错误的输出。
HI您需要更改php脚本才能将转换后的奖品发送到谷歌转换器尝试以下代码并检查
//getting the currency values from session.
session_start();
//old currency
$from=(!empty($_SESSION['old_currency']))?$_SESSION['old_currency']:'INR';
echo 'From: '.$from.'<br>';
//new currency
$to=(!empty($_SESSION['new_currency']))?$_SESSION['new_currency']:'INR';
echo 'To: '.$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;
}
我希望这能帮助
嗨,你会向谷歌服务发送相同的货币吗?
从更改您的支票
if ($from == $to)
至
if (strtoupper($from) == strtoupper($to))