2016-05-16 103 views
0

我有一個表單,它在我的外部php文件中調用convert currency函數。我想調用這個函數我ajax。我的目標是當用戶輸入他希望轉換的數量時,應該顯示結果而不用按提交按鈕。當用戶輸入他的金額時,我想讓ajax與我的php函數進行通信。下面是我的代碼,需要你的幫助。如何在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函數直接嘗試,但沒有happaens

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); 
} 
+0

試着把hr.open(「POST」,url,true);在發送前 –

+0

也應該是setRequestHeader而不是sendRequestHeader? –

回答

0

2行:你的JavaScript代碼。我想你的意思是var hr = new XMLhttpRequest();。你在那裏錯過了一個「q」。

+0

我糾正了缺失的q,但沒有發生任何事情。你認爲我的PHP文件是錯誤的嗎? – Wateya

+0

**第56行:您的第一個代碼段**。 POST ['to']應該是$ _POST ['to'] – Fredster