girlzz Posted August 26, 2008 Share Posted August 26, 2008 dear sir, how can i make calculation then the total will appear automatically in the textfield??? for example: textfield 1 + textfield 2 divide 2 = textfield total... please help me Quote Link to comment Share on other sites More sharing options...
haku Posted August 26, 2008 Share Posted August 26, 2008 You don't need Ajax for this. Where are the three numbers coming from? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 26, 2008 Share Posted August 26, 2008 var total = (document.getElementById("textfield1").value + document.getElementById("textfield2").value) / 2; document.getElementById("total").value = total; Quote Link to comment Share on other sites More sharing options...
kjtocool Posted August 26, 2008 Share Posted August 26, 2008 dear sir, relying on others to write the code for you without so much as an attempt to solve the problem on your own will generally not fly. Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 27, 2008 Author Share Posted August 27, 2008 sory sir...i'm new to this calculation... if there any link so that i can learn step by step about how to calculate, i'm so thankful Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 27, 2008 Author Share Posted August 27, 2008 You don't need Ajax for this. Where are the three numbers coming from? those two number i have to insert myself then the total of those two number will automatic itself appear.. without using any button... Quote Link to comment Share on other sites More sharing options...
haku Posted August 27, 2008 Share Posted August 27, 2008 Look at the code project fear posted. It will do what you want. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 This is better: Number 1: <input type='text' id='textfield1' value='Enter a number' onfocus='this.value=""; emptyTotal();' onblur='this.value = this.value==""? "Enter a number" : this.value;' size='25' /><br /> Number 2: <input type='text' id='textfield2' value='Enter a number' onfocus='this.value=""; emptyTotal();' onblur='this.value = this.value==""? "Enter a number" : this.value;' size='25' /><br /> Total: <input type='text' id='textTotal' value='Click to see the total' size='25' onclick='calculateAverage();' readonly /> <script> function emptyTotal () { document.getElementById("textTotal").value = "Click to see the total"; } function calculateAverage () { var Calculator = { doc_getID : function (id) { return document.getElementById(id); }, emptyField : function (field) { if (field === "Enter a number" || field === "") { return true; } return false; }, NAN : function (field) { if (typeof field == "string") return isNaN(field); if (typeof field == "number") return false; return true; } } var textfield1 = Calculator.doc_getID("textfield1").value, textfield2 = Calculator.doc_getID("textfield2").value; if (Calculator.emptyField(textfield1)) { alert("Number 1 is empty."); return; } if (Calculator.emptyField(textfield2)) { alert("Number 2 is empty."); return; } if (Calculator.NAN(textfield1)) { alert("Number 1 is not a number"); return; } if (Calculator.NAN(textfield2)) { alert("Number 2 is not a number"); return; } Calculator.doc_getID("textTotal").value = (parseInt(textfield1) + parseInt(textfield2)) / 2; } Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 28, 2008 Author Share Posted August 28, 2008 <script type="text/javascript"> function calculate() { var total = (parseInt(document.getElementById("cam").value) + parseInt(document.getElementById("final_exam").value)) / 2; document.getElementById("total").value = total; } </script> i got this code..it's work but it work if only there is one data.... what if my data had many to calculate?? i'm so stuck.. i made my form using php also... so it's has repeat region because all my data are retrive from the database..just the calculation insert manually from admin and the total is auto appear.. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 What do you mean one data? Try this: function calculate () { var Calculator = { emptyField : function (field) { if (field === "Enter a number" || field === "") { return true; } return false; }, NAN : function (field) { if (typeof field == "string") return isNaN(field); if (typeof field == "number") return false; return true; } } var _len = arguments.length, _start = -1, _sum = 0; while (++_start < _len) { var thisarg = arguments[_len]; if (Calculator.emptyField(thisarg) { alert("Number " + _len + " is empty."); return; } if (Calculator.NAN(thisarg)) { alert("Number " + _len + " is not a number."); return; } _sum += parseInt(thisarg); } _sum /= 2; document.getElementBy("total").value = _sum; } To use it, just do this: var cam = document.getElementById("cam").value; var final_exam = document.getElementById("final_exam").value; calculate(cam, final_exam); Add as many as you want. Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 28, 2008 Author Share Posted August 28, 2008 <script type="text/javascript"> function calculate() { var total = (parseInt(document.getElementById("cam").value) + parseInt(document.getElementById("final_exam").value)) / 2; document.getElementById("total").value = total; } </script> this is my php code: there is do that's mean there is a a lot of data to be calculate <?php do { ?> <tr> <td height="20" scope="col"><input name="matric_no[]" type="text" id="matric_no[]" value="<?php echo $row_test['matric_no']; ?>" size="10" readonly="true" /></td> <td scope="col"><input name="cam" type="int" id="cam" size="8" /></td> <td scope="col" ><input name="final_exam" type="int" id="final_exam" size="8" /></td> <td scope="col"><input name="total" type="int" id="total" size="8" onClick="calculate()" readonly="true"/></td> <td scope="col"><input name="grade" type="text" id="grade" size="8" onClick="gred()" readonly="true"/></td> </tr> <?php } while ($row_test = mysql_fetch_assoc($test)); ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 Try this: PHP: <?php $count = 0; while ($row_test = mysql_fetch_assoc($test)) { echo "<tr> <td height='20' scope='col'><input name='matric_no[]' type='text' id='matric_no" . $count . "' value='" . $row_test['matric_no'] . "' size='10' readonly /></td> <td scope='col'><input name='cam[]' type='text' id='cam" . $count . "' size='8' /></td> <td scope='col'><input name='final_exam[]' type='text' id='final_exam" . $count . "' size='8' /></td> <td scope='col'><input name='total[]' type='text' id='total" . $count . "' size='8' onClick='calculate(" . $count . ");' readonly /></td> <td scope='col'><input name='grade[]' type='text' id='grade" . $count . "' size='8' onClick='gred(" . $count . ");' readonly /></td> </tr>"; ++$count; } ?> JavaScript: function calculate (id) { var Calculator = { doc_getID : function (id) { return document.getElementById(id); }, emptyField : function (field) { if (field === "Enter a number" || field === "") { return true; } return false; }, NAN : function (field) { if (typeof field == "string") return isNaN(field); if (typeof field == "number") return false; return true; } } var field1 = Calculator.doc_getID("cam" + id).value, field2 = Calculator.doc_getID("final_exam" + id).value; if (Calculator.emptyField(field1)) { alert("Number " + id + " is empty."); return; } if (Calculator.emptyField(field2)) { alert("Number " + id + " is empty."); return; } if (Calculator.NAN(field1)) { alert("Number " + id + " is not a number"); return; } if (Calculator.NAN(field2)) { alert("Number " + id + " is not a number"); return; } Calculator.doc_getID("total" + id).value = (parseInt(field1) + parseInt(field2)) / 2; } Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 28, 2008 Author Share Posted August 28, 2008 not working... hurm never mind.. by the way thanks for your help Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 28, 2008 Share Posted August 28, 2008 not working... hurm never mind.. by the way thanks for your help Any errors? Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 28, 2008 Author Share Posted August 28, 2008 Parse error: syntax error, unexpected $end in C:\AppServ\www\mas\aug18\aug18\mas\examtry.php on line 377 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 That error occurs usually when you are missing a } somewhere in your script. Quote Link to comment Share on other sites More sharing options...
girlzz Posted August 29, 2008 Author Share Posted August 29, 2008 i had tried insert the missing one but still came out with error Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 29, 2008 Share Posted August 29, 2008 i had tried insert the missing one but still came out with error Could you show us the code? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.