Click here to Skip to main content
14,920,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table currency, in it have column currency (USD) and column ExchRate (100.00)
in my dropdown list. i want to show currency value. after i click on the dropdown list (using 'onchange'), the result of ExchRate will be display in readonly form below the dropdown. i saw some tutorial using ajax and function, but i dont really understand how it catch and fetch the data.

What I have tried:

<select id="exchangerate" name='exchangerate' onchange="initExchRate()">
							<option value="">-</option>
							<?	//query for dropdown currency
								$stroption = "SELECT * FROM Currency";
								$rstoption = mysql_query($stroption);
								
								while($objoption = mysql_fetch_object($rstoption)){
									echo "<option value='".$objoption->ExchRate."'>".$objoption->Currency."</option>";
								}
							?>
						</select>
						<input name="txtRate1" type="text" id="txtRate1" size="15" maxlength="20" class="txb" tabindex="<?=$tabindex++?>"readonly>


function initExchRate(){
	var exchangerate = $("#exchangerate").val();
	var rate = document.getElementById('txtRate1');
	var options = new Object();

	var ajaxy = $.ajax({
		method: "GET",
		url: "../../../script/ajaxphp3.php",
		dataType: "JSON",
		data: {
			getCase: "getExchangerate",
			Exchangerate: exchangerate
		}
	})
	.done(function(data){
		var exchrate = data.ExchRate;

		getExchRate();
	});
}

function getExchRate(){
	var exchangerate = $("#exchangerate").val();
	var excRate = $("#txtRate1").val();

	var ajaxy = $.ajax({
		method: "GET",
		url: "../../../script/ajaxphp3.php",
					dataType: "JSON",
					data: { 
						getCase: "getExcRate",
						Exchangerate: exchangerate
						ExcRate: excRate
					}
				})
				.done(function(data){
					setExcRateFields(data);
				});
}
function setExcRateFields(Currency){

	document.getElementById("txtRate1").value = Currency.ExchRate;
}
Posted
Updated 17-Feb-21 22:07pm

1 solution

You don't need AJAX; you've already got all the data you need.
JavaScript
function displayExchangeRate(){
    var value = $("#exchangerate").val();
    $("#txtRate1").val(value);
}

$("#exchangerate").change(displayExchangeRate);
displayExchangeRate();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900