...

View Full Version : Percentage Calculator HELP



Reveal
07-15-2012, 11:36 AM
So here is my code so far:

<SCRIPT language=JavaScript>
function perc1() {
a = document.form1.a.value/100;
b = a*document.form1.b.value;
document.form1.total1.value = b
}
function perc2() {
a = document.form1.c.value;
b = document.form1.d.value;
c = a/b;
d = c*100;
document.form1.total2.value = d
}
</SCRIPT>

<div><span style="font-size: 24px; font-weight: bold; text-decoration: underline;"><br />
</span>
<form name="form1">
<table cellspacing="1" cellpadding="1" border="0">
<tbody>
<tr>
<td>Exchanges under $100 =
<input type="hidden" size="5" value="25" name="a" />25% of
<input size="5" name="b" />? </td>
<td>Answer:
<input maxlength="40" size="5" name="total1" disabled="disabled" /></td>
<td>
<input onclick="perc1()" type="button" value="Calculate" /></td>
</tr>
</tbody>
</table>
</form></div>

This basically works out 25% of the input, now how I do make a new tr tag that will auto substract the input(b) - the percentage answer (total1) and display the value

Philip M
07-15-2012, 11:50 AM
<input maxlength="40" size="5" name="total1" disabled="disabled" /></td>

Why disabled? Do you perhaps intend readonly?

a = document.form1.a.value/100;

Do not use the same name for an HTML form element and a Javascript variable. Some browsers will get confused. It is very poor practice to assign meaningless names to elements and variables which give no clue as to what they are supposed to be. And what is the pont of having this value in a hidden field? And its value is 25 (a number), not "25" (a string). That can be fatal if a + sign is involved.

You have omitted the var keyword so all the variables are global and those in perc1() will be over-written by those of the same name in perc2().

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

Just make a new table cell and write the result of your calculation to it. What is your problem here?

It is your responsibility to die() if necessary….. - PHP Manual

Reveal
07-15-2012, 11:53 AM
<input maxlength="40" size="5" name="total1" disabled="disabled" /></td>

Why disabled? Do you perhaps intend readonly?

a = document.form1.a.value/100;

Do not use the same name for an HTML form element and a Javascript variable. Some browsers will get confused.

You have omitted the var keyword so all the variables are global and those in perc1() will be over-written by those of the same name in perc2().

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

Just make a new table cell and write the result of your calculation to it. What is your problem here?

It is your responsibility to die() if necessary….. - PHP Manual

Yes the field is read only

Still doesn't answer my questions:
How do I:input(b) - the percentage answer (total1) and display the value

Philip M
07-15-2012, 11:59 AM
Yes the field is read only

Still doesn't answer my questions:
How do I:input(b) - the percentage answer (total1) and display the value

One more time, just make a new table cell and write the result of your calculation to it. What is your problem here?

But it appears that b and total1 are the same value:-

b = a*document.form1.b.value;
document.form1.total1.value = b


As this is clearly homework you write the code and then someone will doubtless correct/improve it for you. But first you must make the corrections to the errors that I have pointed out.

Reveal
07-15-2012, 12:02 PM
One more time, just make a new table cell and write the result of your calculation to it. What is your problem here?

As this is clearly homework you write the code and then someone will doubtless correct/improve it for you.

Oh great forum, not.

it is a form that finds out exchange rates, for example someone wanting to exchange 100 Payza to "Liberty Reserve" the fee will be 25% which is 25

So I want to create a code/tr that will show the result of the field of Value Entered - Percentage Total1 which I don't know how to do

Philip M
07-15-2012, 12:05 PM
Oh great forum, not.


Well, that ends any further help from me. :) Have a look at Forum Rule 1.5 about homework assignments.



var rude = true;
var thanks = false;
if (rude && !thanks) {
var interestLost = true;
var moreHelp = 0;
}

Reveal
07-15-2012, 12:07 PM
Well, that ends any further help from me. :) Have a look at Forum Rule 1.5 about homework assignments.

No thanks, enjoy downtime on forum D:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum