簡體   English   中英

我如何在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);
}

第2行:您的JavaScript代碼。 我認為您的意思是var hr = new XMLhttpRequest(); 您錯過了一個“ q”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM