我有一个调用php函数的表单,并在按钮被提交时返回转换后的货币。我需要在ajax中调用这个php函数并返回结果(结果显示在textfield结果中)而不按提交按钮。我希望当用户输入他想要转换的金额时,无需按下提交按钮即可自动调用该功能。下面是我的表单和php函数:
<form class="form-signin" action="" method="post">
<h4 class="form-signin-heading"style="color:white;">Check Our Rates Here!</h4>
<Strong style="color:white;">i have:</strong>
<select class="form-control" name="from"id="from">
<option value="<?php echo getcurrency();?>"><?php echo getcurrency();?></option>
<option value="USD">American Dollar(USD)</option>
<option value="GBP">Britsh Pound(GBP)</option>
</select>
<input type="text" name="amount"id="amount"class="form-control" Amount="Your email"required autofocus>
<Strong style="color:white;">Reciver Gets:</strong>
<select class="form-control" name="to"id="to" >
<option value="USD">American Dollar(USD)</option>
<option value="GBP">Britsh Pound(GBP)</option>
<option value="UGX">uganda shillings (UGX)</option>
<option value="KES">Kenya Shilling(KES)</option>
<option value="EUR">Euro(EUR)</option>
</select>
<input class="form-control" id="results"name="results"value="<?php if(isset($_POST['amount'],$_POST['from'],$_POST['to'])){
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];
$currency = currencyConverter($from,$to,$amount);
echo $currency;
}?>">
<button class="btn btn-lg btn-primary btn-block" name="submit"type="submit">Convert</button>
</form>
这是我的php货币转换功能
function currencyConverter($from,$to,$amount){
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from yahoo.finance.xchange where pair in ("'.$from.$to.'")';
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$yql_session = file_get_contents($yql_query_url);
$yql_json = json_decode($yql_session,true);
$currency_output = (float) $amount*$yql_json['query']['results']['rate']['Rate'];
if($from=='AED'&&$to=='USD'){
$aedtousd='4';
$finalaedtousd = $currency_output-$aedtousd;
return $finalaedtousd;
}else if($from=='USD'&&$to=='AED') {
$aedtousd ='0.033';
return $currency_output-$aedtousd;
}else if($from=='AED'&&$to=='UGX'){
$eadtougx ='18';
return $currency_output-$eadtougx;
}else{
return $currency_output;
}
}
?>