# Thread: Show percent of total before insert?

1. ## Show percent of total before insert?

Hello

I'm sorry if this is the wrong subforum – I'm not sure that this is possible to do with PHP.

Inside a form I have three textfields:

Total
Woman
Men

Is it possible to show the percent of woman / men when you fill in the textfields? That is before you submit the form.

For example: you fill in "100" in total, "30" in woman – and "30" is shown besides the textfield "woman".

I have tried writing my own function, but I get a warning: "Division by zero"

Anyone who can point me in the right direction for a solution?

Regards
Magnus

2. Since you are trying to achieve this before submitting the form, this has to be done on the client side in Javascript and not in php.

if you have to calculate the percent of women/men, "men" should have a non-zero value, otherwise you will get the divide by zero error. As a good practice, before dividing by any number, you should always check that the denominator is not zero.

In your case, if the only inputs are total and women, you should first calculate

men = total - women

and then calculate the percent.

3. Example using YAHOO user Interface library.

Code Javascript:
```//YUI object shortcuts
var \$D = YAHOO.util.Dom;
var \$E = YAHOO.util.Event;
var \$ = \$D.get;

//Listen to women blur event
function init() {
\$E.on(['total', 'women'], 'blur', setPercentage);
}

//Calculate women percentage
function setPercentage(e) {
var total = parseInt(\$('total').value);
var women = parseInt(\$('women').value);

if(total == '' || total == 0 || women == '') return; //Rough validation

var percentage = (Math.round((women/total)*10000))/100;
percentage = percentage + ' %';
\$('percentage').innerHTML = percentage;
}

Code HTML:
```<script type="text/javascript" src="http://yui.yahooapis.com/2.3.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>

<form method="post" action="my_script.php">
<p>
Total: <input type="text" name="total" id="total"><br>
Women: <input type="text" name="women" id="women"> <span id="percentage"></span><br>
Men: <input type="text" name="men" id="men">
</p>
</form>```

ideally the total would be derived automatically from men and women fields

4. Thanks both of you

I know close to nothing about Javascript but I will try your script cranial-bore.

Regards,
Magnus

5. One thing, you mentioned a division by zero error. You got this when the total = 0, because you would be doing 0/0. To fix that, just add an if statement something like:
if(total != 0)
// Get the percent.

6. Originally Posted by cranial-bore
ideally the total would be derived automatically from men and women fields
Hello again – and sorry it took so long.

I proberly explaind what I wanted bad (or I'm even worse in Javascript then I thought )

I made this dummy (see screen shot) of how I want it to work. When you tab from field "woman" to field "men" the percent of woman should be displayed next to the field "woman" ("40" in this simple example).

Is this at all possible?

In the database I only save the numbers from the fields.

Regards Magnus

Screen shot:

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•