dpndp64206 2016-05-16 07:51
浏览 62

我怎么能在ajax中调用php函数

i have a form which calls the convert currency function in my external php file. i want to call this function i ajax. my target is as the user types the amount he wishes to convert, the results should be displayed without pressing the submit button. i want ajax to communicate with my php function as the user inputs his amount. below is my code and need your help.

   //------------------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!");
    }
}


  }

I also tried using the javascript function direct but nothing 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);
}
  • 写回答

1条回答 默认 最新

  • douyingp82418 2016-05-16 08:50
    关注

    Line 2: of your javascript code. I think you mean var hr = new XMLhttpRequest();. you missed a "q" there.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?