Jump to content

calculation


girlzz

Recommended Posts

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

<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)); ?>

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.