私は私の外部PHPファイルの通貨変換機能を呼び出すフォームを持っています。私はこの機能をi ajaxと呼んでいます。私の目標は、ユーザーが変換したい金額を入力すると、結果は送信ボタンを押さずに表示されるはずです。私は、ユーザーが彼の金額を入力すると、私のPHP関数と通信する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機能を使用してみましたが、何もライン2
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);
}
hr.open( "POST"、url、true)を入れてみてください。 sendの直前 –
また、setRequestHeaderはsendRequestHeaderではないはずですか? –