0

Guess this is javascript.

Similar as in excel, (I have seen this before), lets say I have 3 fields in a form.

Now when entering a numeric value in field 1, and lets say field 2 has the value of 5, then the value of field 1 times field 2 has to display in filed 3.

Please if anyone could assist me with this.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by R0bb0b
0

Do you just mean entering them into text boxes and you want PHP to add them up whenever you submit?

0

This would be a javascript version

<!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=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
function calcFields()
{
	if(!IsNumeric(document.getElementById("txtVal1").value) || document.getElementById("txtVal1").value == "")
	{
		var val1 = 0;
	}
	else
	{
		var val1 = document.getElementById("txtVal1").value;
	}
	document.getElementById("txtVal1").value = val1;
	if(!IsNumeric(document.getElementById("txtVal2").value) || document.getElementById("txtVal2").value == "")
	{
		var val2 = 0;
	}
	else
	{
		var val2 = document.getElementById("txtVal2").value;
	}
	document.getElementById("txtVal2").value = val2;
	
	document.getElementById("txtVal3").value = parseFloat(val1) + parseFloat(val2);
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
</script>
</head>

<body onload="calcFields();">
<table cellpadding="0" cellspacing="0">
	<tr>
		<td>&nbsp;</td>
		<td><input type="text" name="txtVal1" id="txtVal1" onblur="calcFields();" /></td>
	</tr>
	<tr>
		<td>+</td>
		<td><input type="text" name="txtVal2" id="txtVal2" onblur="calcFields();" /></td>
	</tr>
	<tr>
		<td colspan="2"><hr style="width:100%;" /></td>
	</tr>
	<tr>
		<td>=</td>
		<td><input type="text" name="txtVal3" id="txtVal3" readonly="readonly" /></td>
	</tr>
</table>
</body>
</html>
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.