Hi Friends

I am using dynamic text box, i want to calculate the amount from value of rate in the text box and qty

Pls help me how to solve it...

<?php
	include("config.php");
?>
<script type=text/javascript>
function multiply(){
a = document.getElementById("rate").value;
b = document.getElementById("qty").value;
c = a*b;
amt = 
alert(c);
}
</script>
<!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>Untitled Document</title>

</head>

<body>
<form action="" method="get" name="test">
<table width="800" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">S.No</td>
    <td align="center">Item Name</td>
    <td align="center">Item Code</td>
    <td height="30" align="center">Rate</td>
    <td align="center">QTY</td>
    <td align="center">Amount</td>
    </tr>
  <tr>
  <?php $desr="select * from site_rate where category_code='EWS001' AND sitename='Amber' order by item_code";
		//echo $desr;
	  	$r=mysql_query($desr,$conn);
	  	$i=1;
		while($m=mysql_fetch_array($r))
		{
			?>
    <td><?php echo $i; ?></td>
    <td><textarea name="cat_item" cols="25" rows="2"><?php echo $m['category_item'];?></textarea></td>
    <td><?php echo $m['item_code'];?></td>
    <td><input name="rate" type="text" id="rate"/></td>
    <td><input name="qty" type="text" id="qty" onchange="multiply()"/></td>
    <td><input name="amt" type="text" /></td>
    </tr><?php $i++; } ?>
</table>

</form>

</body>
</html>

You cannot have multiple tags with the same id. Your code will output multiple instances of <input id="rate"> and <input id="qty">.

You should serialise them, like this:

<?php
	include("config.php");
?>
<script type=text/javascript>
function multiply(id){
var a;
var b;
var c;
a = document.getElementById("rate" + id).value;
b = document.getElementById("qty" + id).value;
document.getElementById("amt" + id).value = a * b;
}
</script>
<!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>Untitled Document</title>

</head>

<body>
<form action="" method="get" name="test">
<table width="800" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">S.No</td>
    <td align="center">Item Name</td>
    <td align="center">Item Code</td>
    <td height="30" align="center">Rate</td>
    <td align="center">QTY</td>
    <td align="center">Amount</td>
    </tr>
  <tr>
  <?php $desr="select * from site_rate where category_code='EWS001' AND sitename='Amber' order by item_code";
		//echo $desr;
	  	$r=mysql_query($desr,$conn);
	  	$i=1;
		while($m=mysql_fetch_array($r))
		{
			?>
    <td><?php echo $i; ?></td>
    <td><textarea name="cat_item" cols="25" rows="2"><?php echo $m['category_item'];?></textarea></td>
    <td><?php echo $m['item_code'];?></td>
    <td><input name="rate<?php echo $i; ?>" type="text" id="rate<?php echo $i; ?>"/></td>
    <td><input name="qty<?php echo $i; ?>" type="text" id="qty<?php echo $i; ?>" onchange="multiply(<?php echo $i; ?>)"/></td>
    <td><input name="amt<?php echo $i; ?>" type="text" id="amt<?php echo $i; ?>" /></td>
    </tr><?php $i++; } ?>
</table>

</form>

</body>
</html>

Thanks for your reply,

<td><input name="rate<?php echo $i; ?>" type="text" id="rate<?php echo $i; ?>"/></td>

Suppose if i use

<td><input name="rate[]" type="text" id="rate[]"/></td>

is it possible to get the value

Yes, you can for the name attribute but no not for the id attribute.
But in any event, the name attribute is never used because the form is never submitted; from what I can tell you are simply using input boxes to display information. The only reason to keep the name attribute is to produce HTML that is W3C compliant.

Hi

ok, i got output with your help.

Thanks for your interest and your valuable time

You are welcome. Please don't forget to mark this thread "Solved" when ready.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.