如何在ajax中调用php函数


how can i call php function in ajax

我有一个表单,它在我的外部php文件中调用convert currency函数。我想把这个函数称为ajax。我的目标是,当用户键入他希望转换的金额时,应该在不按下提交按钮的情况下显示结果。我希望ajax在用户输入金额时与我的php函数通信。下面是我的代码,需要您的帮助。

   //------------------convert currency form--------
<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" onclick="process()"placeholder="the amount here"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">
        <button class="btn btn-lg btn-primary btn-block" name="submit"type="submit">Convert</button>
    </form>
   //-------------php file---------------
header('Content-type:text/xml');
echo'<?xml version="1.0"encoding="UTF-8"standalone="yes"?>';
echo '<response>';
function currencyConverter($ffrom,$to,$amount){
            $yql_base_url = "http://query.yahooapis.com/v1/public/yql";
            $yql_query = 'select * from yahoo.finance.xchange where pair in     ("'.$ffrom.$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($ffrom=='AED'&&$to=='USD'){
                $aedtousd='4';
                $finalaedtousd = $currency_output-$aedtousd;
                    return $finalaedtousd;  
            }else if($ffrom=='USD'&&$to=='AED') {
                $aedtousd ='0.033';
                return $currency_output-$aedtousd;
            }else if($ffrom=='AED'&&$to=='UGX'){
                $eadtougx ='18';
                return $currency_output-$eadtougx;
            }else{
                return $currency_output;
            }
            }
            if(isset($_POST['amount'],$_POST['from'],POST['to'])){
            $amount = $_POST['amount'];
             $ffrom = $_POST['from'];
             $to = $_POST['to'];
         $currency = currencyConverter($ffrom,$to,$amount);
            echo $currency;
        }
 echo '</response>';
?>
            //-------------Javascript-----------------------------
            var xmlHttp = createXmlHttpRequestObject();
             function createXmlHttpRequestObject(){
             var xmlHttp;
               if(window.ActiveXObject){
    try{
        xmlHttp = ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        xmlHttp = false;
    }
}else{
    try{
    xmlHttp = new XMLHttpRequest();
    }catch(e){
        xmlHttp = false;
    }
}
if(!xmlHttp){
    Alert("sorry we couldnt process your request");
}else{
    return xmlHttp;
}

   }
 function process(){
if(xmlHttp.readyState== 0 || xmlHttp.readyState== 4){
    ffrom = encodeURIComponent(document.getElementById("from").value);
    amount = encodeURIComponent(document.getElementById("amount").value);
    to = encodeURIComponent(document.getElementById("to").value);
    xmlhttp.open("GET","core/curencajax.php?ffrom=" + ffrom + "&to=" + to +"&amount"+ amount, true);
    xmlhttp.onreadystatechange = handleServerResponse;
    xmlhttp.send(null);
}else{
    setTimeout('process()',1000);
}
}

  function handleServerResponse(){
if(xmlHttp.readyState== 4){
    if(xmlHttp.status==200){
        xmlResponse = xmlHttp.responseXML;
        xmlDocumentElement = xmlResponse;
        message = xmlDocumentElement.firstChild.data;
        document.getElementById("results").innerHTML = message;
        setTimeout('process()',1000);
    }else{
        Alert("sorry something went wrong!");
    }
}

  }

我也尝试过直接使用javascript函数,但没有成功的

function process(){
var hr = new XMLhttpReuest();
var url = "core/curencajax.php";
var ffrom = document.getElementsById("from").value;
var amount = document.getElementsById("amount").value;
var to = document.getElementsById("to").value;
var vals = "ffrom="+ffrom+"&amount="+amount+"&to"+to;
hr.open("POST",url,true);
hr.sendRequestHeader("Content-type","application/x-www-form-urlencoded");
hr.onreadystatechange= function(){
    if(hr.readyState==4 && hr.status==200){
        var return_data = hr.responseText;
        document.getElementById("results").innerHTML = return_data;
        }
    }
    hr.send(vals);
}
javascript代码的第2行:。我想你是指var hr = new XMLhttpRequest();。你错过了一个"q"。