Where have you declared 'slopetop', 'slopebottom' and 'slopefinal'? And document.write() is evaluated as the page is being rendered. Plus Javascript is a client side technology; as soon as a page is submitted, what you get is a new page and the state maintained in Javascript variables is reset.
You need to dynamically update the value of a span or div element to get this code working as expected. Something like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Expires" content="0" /> <!-- disable caching -->
<title>Example</title>
<script type="text/javascript">
function calculate(frm) {
if(!frm)
return(false);
var elems = frm.elements;
var x1 = parseInt(elems['inpx1'].value, 10);
var x2 = parseInt(elems['inpx2'].value, 10);
var y1 = parseInt(elems['inpy1'].value, 10);
var y2 = parseInt(elems['inpy2'].value, 10);
var e = elems['result1'];
alert("(" + x1 + "," + y1 + ")" + " and (" + x2 + "," + y2 + ")");
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
e.value = isNaN(distance) ? 'Invalid input' : distance;
}
</script>
</head>
<body>
<form action="#">
<div>(<input type="text" name="inpx1" size="5">,<input type="text" name="inpy1" size="5">)</div>
<div>(<input type="text" name="inpx2" size="5">,<input type="text" name="inpy2" size="5">)</div>
<input type="button" onclick="return calculate(this.form);" value="Calculate!">
<div>Distance: <input name="result1" readonly="readonly"></div>
</form>
</body>
</html> ~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734