简单的汇率转换工具—初试AJAX
今天自己折腾了一个简单的汇率转换工具,顺便简单的学习了一下AJAX技术,在这里做个记录,以便自己以后查询。废话不多说,直接上代码:
convert.PHP文件内容:
<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<title>汇率转换</title>
<Meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
</head>
<body ontouchstart="">
<div class="wxapi_container">
<div class="wxapi_index_container">
<table>
<tr>
<td>从:</td>
<td>
<select id="select1">
<option value="USD" selected = "selected">美元</option>
<option value="EUR">欧元</option>
<option value="GBP">英镑</option>
<option value="AUD">澳元</option>
<option value="CAD">加元</option>
<option value="HKD">港元</option>
<option value="CNY">人民币</option>
</select>
</td>
</tr>
<tr>
<td>到:</td>
<td>
<select id="select2">
<option value="USD">美元</option>
<option value="EUR">欧元</option>
<option value="GBP">英镑</option>
<option value="AUD">澳元</option>
<option value="CAD">加元</option>
<option value="HKD">港元</option>
<option value="CNY" selected = "selected">人民币</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="button" value="转换" onclick="convert()"></td>
</tr>
<tr>
<td>结果:</td>
<td><input type="text" id="result" value="0" readonly="true"></td>
</tr>
</table>
</div>
</div>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script> /** * 普通Ajax的完整访问过程 */ var xmlHttp function convert()//登录转换点击后执行这个方法 { var select1=document.getElementById("select1").value; var select2=document.getElementById("select2").value; if (select1 == select2 )//不能从人民币转换为人民币 { document.getElementById("result").value="请选择不同的货币单位"; return } xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="change.PHP"//设置要提交action到后台的那个处理请求的文件名 url=url+"?scur="+select1+"&tcur="+select2//为这个路径加上参数 url=url+"&sid="+Math.random()//为这个路径加上一个随机数 xmlHttp.onreadystatechange=stateChanged//每当 readyState 改变时,就会触发 onreadystatechange 事件,readyState 属性存有 XMLHttpRequest 的状态信息 xmlHttp.open("GET",url,true)//定义请求的参数 xmlHttp.send(null)//发送请求 } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") //0: 请求未初始化 //1: 服务器连接已建立 //2: 请求已接收 //3: 请求处理中 //4: 请求已完成,且响应已就绪 { var a= xmlHttp.responseText;//把相应数据赋值给a var resultinput=document.getElementById("result"); resultinput.value = a; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox,Opera 8.0+,Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } </script>
</html>
<?PHP
//header("Content-Type:text/html;charset=UTF-8");
function nowapi_call($a_parm){
if(!is_array($a_parm)){
return false;
}
//combinations
$a_parm['format']=empty($a_parm['format'])?'json':$a_parm['format'];
$apiurl=empty($a_parm['apiurl'])?'http://api.k780.com:88/?':$a_parm['apiurl'].'/?';
unset($a_parm['apiurl']);
foreach($a_parm as $k=>$v){
$apiurl.=$k.'='.$v.'&';
}
$apiurl=substr($apiurl,0,-1);
if(!$callapi=file_get_contents($apiurl)){
return false;
}
//format
if($a_parm['format']=='base64'){
$a_cdata=unserialize(base64_decode($callapi));
}elseif($a_parm['format']=='json'){
if(!$a_cdata=json_decode($callapi,true)){
return false;
}
}else{
return false;
}
//array
if($a_cdata['success']!='1'){
echo $a_cdata['msgid'].' '.$a_cdata['msg'];
return false;
}
return $a_cdata['result'];
}
//$nowapi_parm['app']='finance.rate';
//$nowapi_parm['scur']='GBP';
//$nowapi_parm['tcur']='CNY';
///$nowapi_parm['appkey']='10003';
//$nowapi_parm['sign']='b59bc3ef6191eb9f747dd4e83c99f2a4';
//$nowapi_parm['format']='json';
//if(!$result=nowapi_call($nowapi_parm)){
// die('fail');
//}
//var_dump($result);
//print_r($result);
//echo " scur : ".$result['scur']."<br>";
//echo " tcur : ".$result['tcur']."<br>";
//echo " ratenm : ".$result['ratenm']."<br>";
//echo " rate : ".$result['rate']."<br>";
//echo " update : ".$result['update']."<br>";
function convert($scur,$tcur){
$nowapi_parm['app']='finance.rate';
$nowapi_parm['scur']= $scur;
$nowapi_parm['tcur']= $tcur;
$nowapi_parm['appkey']='10003';
$nowapi_parm['sign']='b59bc3ef6191eb9f747dd4e83c99f2a4';
$nowapi_parm['format']='json';
if(!$result=nowapi_call($nowapi_parm)){
return "转换失败!";
}
else{
return $result['rate'];
}
}
$scur=$_GET["scur"];
$tcur=$_GET["tcur"];
//file_put_contents("./test.txt","scur = " . $scur . "\n",FILE_APPEND);
//file_put_contents("./test.txt","tcur = " . $tcur . "\n","result = " . convert($scur,$tcur) . "\n",FILE_APPEND);
echo convert($scur,$tcur);
?>
简单的效果图如下: