Auto Calculation Solution

I have a form where some values are pulled from a database, but I will use a static form for example.

I have a form where I need to choose a value from a dropdown and have calculations performed and populate a hidden field.

<form id="form1 name="form1" method="" action="">
<select name="service">
<option>10</option>
<option>20</option>
<option>30</option>
</select>
<input type="text" name="totalHours" />
<input type="hidden" name="totalCost" />
</form>

My form is more complex but I should be able to build once I get started.

Example: If I choose 10 from the select menu and then enter 1.5 in the totalHours textbox, I need the page to automatically populate the hidden field with the 10 x 1.5 hours. I am not sure the best way to approach this. My pages are .php.

Any solutions?
LVL 16
rbudjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
Hello,

you need to do this either throw java script or ajax. and I recommend java script in your case.
0
PranjalShahCommented:
And you rather than using input type hidden, I will suggest to use CSS display property. You can hide by default and onchange of totalHours show totalCost.

For you javascript auto calculation....


<input type="text" name="totalHours" onChange="autoSum();document.form1.totalCost.style.display='inline';" />
<input type="text" name="totalCost" style="display:none;" />
function autoSum() {
    document.form1.totalCost.value = (document.form1.service.value) * (document.form1.totalHours.value);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lukasz ChmielewskiCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.