Dear friends,
Please consider the following concern.
Suppose I have a 5 rows 3 columns form fields. When I will put any values in Col1 and Col2; Sum Column will calculate sum operation like Col1+Col2. Due to PHP and MySql issue, I had to make the input field as array element. But entry time the sum value will generate by JavaScript.
<form method=”post” action=”process.php” id=”frmArithmetic”>
<input name=”txtCol1[]” type=”text” onBlur=”doSum()” />
<input name=”txtCol2[]” type=”text” onBlur=”doSum()” />
<input name=”txtTotal[]” type=”text” value=”” readonly=readonly/>
<input type=”Submit” name=”btnSave” id=”btnSave” value=”Save”/>
Col1 Col2 Sum
1 ——— 2 ——— 3
2 ——— 5 ——— 7
Any JavaScript solution will be appreciated.
Regards,
Iqbal
http://www.w3schools.com/js/js_operators.asp [/QUOTE]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--
function doSum() {
var col1 = isNaN(parseFloat(document.getElementById('txtCol1[]').value))? 0 : parseFloat(document.getElementById('txtCol1[]').value);
var col2 = isNaN(parseFloat(document.getElementById('txtCol2[]').value))? 0 : parseFloat(document.getElementById('txtCol2[]').value);
document.getElementById("txtTotal[]").value = col1 * col2;
}
//-->
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" type="text" onBlur="doSum()" />
<input name="txtCol2[]" type="text" onBlur="doSum()" />
<input name="txtTotal[]" type="text" value="" readonly='readonly' />
<input type="Submit" name="btnSave" id="btnSave" value="Save"/>
</form>
</body>
</html>
[/code]
maybe use this as a starting point and guide:
[code=php]
[/QUOTE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--
function doSum() {
var col1 = isNaN(parseFloat(document.getElementById('txtCol1[]').value))? 0 : parseFloat(document.getElementById('txtCol1[]').value);
var col2 = isNaN(parseFloat(document.getElementById('txtCol2[]').value))? 0 : parseFloat(document.getElementById('txtCol2[]').value);
document.getElementById("txtTotal[]").value = col1 * col2;
}
//-->
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" type="text" onBlur="doSum()" />
<input name="txtCol2[]" type="text" onBlur="doSum()" />
<input name="txtTotal[]" type="text" value="" readonly='readonly' />
<input type="Submit" name="btnSave" id="btnSave" value="Save"/>
</form>
</body>
</html>
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--
function doSum() {
var col1 = isNaN(parseFloat(document.getElementById('txtCol1').value))? 0 : parseFloat(document.getElementById('txtCol1').value);
var col2 = isNaN(parseFloat(document.getElementById('txtCol2').value))? 0 : parseFloat(document.getElementById('txtCol2').value);
document.getElementById("txtTotal").value = col1 * col2;
}
//-->
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" id="txtCol1" type="text" onBlur="doSum()" />
<input name="txtCol2[]" id="txtCol2" type="text" onBlur="doSum()" />
<input name="txtTotal[]" id="txtTotal" type="text" value="" readonly='readonly' />
<input type="Submit" name="btnSave" id="btnSave" value="Save"/>
</form>
</body>
</html>
[/code]
<i>
</i><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function doSum(f){
var v1=Number(f['txtCol1[]'].value);
var v2=Number(f['txtCol2[]'].value);
f['txtTotal[]'].value=v1+v2;
}
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" type="text" onblur="doSum(this.form)">
<input name="txtCol2[]" type="text" onblur="doSum(this.form)">
<input name="txtTotal[]" type="text" value="" readonly="readonly">
<input type="Submit" name="btnSave" id="btnSave" value="Save">
</form>
</body>
</html>
Under an XHTML Doctype, javascript events must be written in lowercase. same as all the HTML/XHTML attributes. Thus: on[B][COLOR="Blue"]b[/COLOR] [/B]lur, not on[B][COLOR="Blue"]B[/COLOR] [/B]lur
Another error, [B]tirna[/B] : Form's elements have only names, not ids, thus getElementById() will refer nothing. (Except, probably in IE, which, incorrectly, is able to take the name as id, if id is missing)[/QUOTE]
document.getElementById("txtTotal").value = col1[COLOR="Red"]*[/COLOR] col2;
[/QUOTE]
my sincerest apologies :o
I have given my code tester a good spanking and told them if it happens again they're fired ? - basically id's were not added to the input elements
This now works in IE8 and FF3.6
[code=php]
[/QUOTE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--
function doSum() {
var col1 = isNaN(parseFloat(document.getElementById('txtCol1').value))? 0 : parseFloat(document.getElementById('txtCol1').value);
var col2 = isNaN(parseFloat(document.getElementById('txtCol2').value))? 0 : parseFloat(document.getElementById('txtCol2').value);
document.getElementById("txtTotal").value = col1 * col2;
}
//-->
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" id="txtCol1" type="text" onBlur="doSum()" />
<input name="txtCol2[]" id="txtCol2" type="text" onBlur="doSum()" />
<input name="txtTotal[]" id="txtTotal" type="text" value="" readonly='readonly' />
<input type="Submit" name="btnSave" id="btnSave" value="Save"/>
</form>
</body>
</html>
[/code]
Sorry dear, Nothing happened. Would you understand my problem dear?[/quote]
In my script there is no problem with onblur or onBlur. [/QUOTE]
Try this:
[/QUOTE]
<i>
</i><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function doSum(f){
var v1=Number(f['txtCol1[]'].value);
var v2=Number(f['txtCol2[]'].value);
f['txtTotal[]'].value=v1+v2;
}
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" type="text" onblur="doSum(this.form)">
<input name="txtCol2[]" type="text" onblur="doSum(this.form)">
<input name="txtTotal[]" type="text" value="" readonly="readonly">
<input type="Submit" name="btnSave" id="btnSave" value="Save">
</form>
</body>
</html>
Both trina and your code only works at last but only for one row. It is not working the next rows.... Just one row is working. And this is my main problem dear.[/QUOTE]
Under an XHTML Doctype, javascript events must be written in lowercase. same as all the HTML/XHTML attributes. Thus: on[B][COLOR=blue]b[/COLOR] [/B]lur, not on[B][COLOR=blue]B[/COLOR] [/B]lur
Another error, [B]tirna[/B] : Form's elements have only names, not ids, thus getElementById() will refer nothing. (Except, probably in IE, which, incorrectly, is able to take the name as id, if id is missing)[/quote]
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<form action="test.php" method="post" id="myID" >
<div>
<input type="text" name="txtTest" />
<input type="submit" value="submit" />
</div>
</form>
</body>
</html>
[/CODE]
[B][U]id's in forms are valid in XHTML [/U][/B][/QUOTE]
Please consider the image attached here... Hope will understand my problem dear friends...[/QUOTE]
You were not attentive. I said that the form's [I]elements[/I] have no id [I]in your code[/I], thus the method [B]getElementById()[/B] has no ids to find as reference. I said nothing about validation.
Before stating something, read attentively what others have written.[/quote]
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<form action="test.php" method="post" id="myID" >
<div>
<input type="text" name="txtTest" id="txtTest" />
<input type="submit" value="submit" />
</div>
</form>
</body>
</html>
[/CODE]
Please consider the image attached here... Hope will understand my problem dear friends...[/quote]
But in any case, id's in form elements is still XHTML Strict valid according to the w3c validator[/QUOTE]
....Form's elements have only names, not ids, thus getElementById() will refer nothing.....
[/QUOTE]
<i>
</i><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function doSum(f){
var v1=f['txtCol1[]'], v2=f['txtCol2[]'], s=f['txtTotal[]'];
for(var i=0;i<v1.length;i++){
s[i].value=Number(v1[i].value)+Number(v2[i].value);
}
}
</script>
</head>
<body>
<form method="post" action="process.php" id="frmArithmetic">
<input name="txtCol1[]" type="text" onblur="doSum(this.form)">
<input name="txtCol2[]" type="text" onblur="doSum(this.form)">
<input name="txtTotal[]" type="text" value="" readonly="readonly">
<br>
<input name="txtCol1[]" type="text" onblur="doSum(this.form)">
<input name="txtCol2[]" type="text" onblur="doSum(this.form)">
<input name="txtTotal[]" type="text" value="" readonly="readonly">
<br>
<input name="txtCol1[]" type="text" onblur="doSum(this.form)">
<input name="txtCol2[]" type="text" onblur="doSum(this.form)">
<input name="txtTotal[]" type="text" value="" readonly="readonly">
<input type="Submit" name="btnSave" id="btnSave" value="Save">
</form>
</body>
</html>
0.1.9 — BETA 5.24