简体   繁体   中英

Populate array of textbox based on selectbox ajax php mysql with some calculations

Iam trying to create a page which has a form which contains Total and Converted Total Value. And this total is generating from a do while loop in php. At top there is a currency drop down. When i select any currency from the dropdown, the page redirects in ajax mode to another php page called currency1.php and get the required values from currency1.php and fill the appropriate values in Converted total value. Following is the markup page:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Currency Values</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#cur').change(function(){
$.get('test1.php',{cur:$(this).val()},function(data){
$('#emp_id').val(data);
 });
});
});
</script>
</head>

<body>
<table border=1><tr>
<td colspan=3>
<select name="cur" id="cur">
<option value="1">US Dollar</option>
<option value="2">Indian Rupee</option>
<option value="3">British Pound</option>
<option value="4">Euro</option>
<option value="5">Singapore Dollar</option>
<option value="6">Australian Dollar</option>
<option value="7">Canadian Dollar</option>
<option value="8">Swiss Franc</option>
<option value="9">Japanese Yen</option>
<option value="10">Malaysian Ringgit</option>
<option value="11">South African Rand</option>
</select>
<input type="text" name="emp_id" id="emp_id" />
</td>
<tr>
<td><b>Total Value</b></td>
<td><b>Converted Value</b></td>
</tr>
<tr>
<td><input type="text" name="box1" id="box1" value="1000"></td>
<td><input type="text" name="con_ver1" id="con_ver1" /></td>
</tr>
<tr>
<td><input type="text" name="box2" id="box2" value="2200"></td>
<td><input type="text" name="con_ver2" id="con_ver2" /></td>
</tr>
<tr>
<td><input type="text" name="box3" id="box3" value="900"></td>
<td><input type="text" name="con_ver3" id="con_ver3" /></td>
</tr>
<tr>
<td><input type="text" name="box4" id="box4" value="3200"></td>
<td><input type="text" name="con_ver4" id="con_ver4" /></td>
</tr>
<tr><td colspan=2>The Records continues..........</td></tr>
</table>
</body>
</html>

My currency1.php which is getting the ajax query is as follows: This page should return back the results in array to the previous page. Iam not getting how to achieve this in array and return back the results in ajax. Currency1.php is as follows:

<?php
 include('config.php');

$sql = "select rate from currency1 where currency='INR' LIMIT 1";

$result_gt = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$list_gt = mysql_fetch_array($result_gt);
$inrvalue=$list_gt['rate'];


if(isset($_REQUEST['cur'])){
  // connection should be on this page  
    $sql = mysql_query("select currency,rate from currency1 where id =".$_REQUEST['cur']);
   $res = mysql_fetch_assoc($sql);
   $rate=$res['rate'];

//$gt=($inrvalue*10)/$res['rate'];

//$gt=($inrvalue*10)/$res['rate'];
//$gt=($inrvalue/$rate)*total[$i];
$gt=($inrvalue/$rate)*box($i);

echo $gt;die;
}
?>

Please help me with the same..

echo the content in json format in currency1.php.

$.ajax({
  url: "currency1.php",
  context: document.body
}).done(function(response) {
 var data =  jQuery.parseJSON( response );
});

Now use the JSON object to change the value in jQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM