使用selectbox ajax php mysql填充一些文本框,并进行一些计算

时间:2021-12-16 21:29:15

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:

我试图创建一个页面,其中包含一个包含总计和转换总值的表单。这总数来自php中的do while循环。顶部有货币下跌。当我从下拉列表中选择任何货币时,页面会以ajax模式重定向到另一个名为currency1.php的php页面,并从currency1.php获取所需的值并在Converted total value中填入适当的值。以下是标记页面:

  <!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:

获取ajax查询的my currency1.php如下所示:此页面应将数组中的结果返回给上一页。我没有得到如何在数组中实现这一点并返回结果在ajax中。 Currency1.php如下:

<?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..

请帮我一样..

1 个解决方案

#1


2  

echo the content in json format in currency1.php.

以currency1.php中的json格式回显内容。

$.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.

现在使用JSON对象来更改jQuery中的值。

#1


2  

echo the content in json format in currency1.php.

以currency1.php中的json格式回显内容。

$.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.

现在使用JSON对象来更改jQuery中的值。