Forum Moderators: open

Message Too Old, No Replies

How to do simple caculations on web page

         

Jon_King

4:27 pm on Feb 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a situation where I am trying to get two numbers input on a page and return a value.

(H*W)*I=X

The visitor would enter H and W. I is constant and then X would be returned as the answer.

How to do this?

Dreamquick

5:30 pm on Feb 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do it on the client side you could use Javascript (see below for example) the upside is the calculation is instant - the downside is the logic is exposed.

Alternately you could use a server-side language such as ASP, PHP etc. the upside is that the logic is protected but the downside is that you have to send the data off for it to be able to return the answer.

Both examples assume the constant is 100 because I'm lazy!


JAVASCRIPT;

Assumes a form called "myform" exists and has three input boxes - "myh", "myw" and "theanswer" (use readonly to make this last one non-editable).

<script type="text/javascript">
function DoCalc(){

if( isNaN( document.myform.myh.value ) ¦¦ isNaN( document.myform.myw.value ) ){

alert( "Bad input!" );

}else{

document.myform.theanswer.value = ( document.myform.myh.value.valueOf() * document.myform.myw.value.valueOf() ) * 100;

}
}
</script>


ASP;

Assumes a form has passed data to the script via either GET or POST and that it includes two bits of data - "myh" and "myw"

<%
If IsNumeric( Request("myh") ) And IsNumeric( Request("myw") ) Then

Response.Write "The answer is "
Response.Write ( Request("myh") * Request("myw") ) * 100

Else

Response.Write "Bad Data"

End If
%>

- Tony