1

I have a small form which is going to be populated from Mysql and human input. What I want to do is populate 3 other fields based on the other ones.

Example:

Total Parts (Mysql)

Labor (User)

Misc (User)

Sub Total (Dynamic total of above)

Tax (Dynamic calc of above - sub * 13%)

Total (Sub + Tax)

I have searched around but can not quite find what I am looking for, and my skills are zero in Javascript/Ajax/Jquery so I haven't been able to modify anything to work, although I have tried miserably.

Can someone help me out on this or point me to a script that may suit my needs.

Thanks

6
  • I understand you are new to all of this, but what platform are you running on for your server? What server side languages are available to you? You are going to need something on the server to answer for the ajax calls and to talk to the database. PHP? Asp.Net? CGI/perl? Python? Commented Feb 16, 2011 at 2:44
  • Based on your other posts in this thread, and you are of this mindset: "honestly just wanted a simple little script to update a form" I would highly suggest you find a developer to work with. There are several sites out there where you can pay a developer to write the code you need. If, however you wish for tutorials to learn how to do this sort of thing, I would edit your question and ask for good starter places to learn this. Commented Feb 16, 2011 at 3:25
  • Why can I not post notes to this instead having to actually answer my own question? Commented Feb 16, 2011 at 21:11
  • @Confused: Because you're trying to post answers from a different account, and the system doesn't know that it is your question. You can always post comments on your own questions and answers. After you earn 50 reputation points, you can post comments anywhere. Commented Feb 16, 2011 at 21:14
  • I have merged your Webmaster account into your Confused account. Please use the Confused account to log in from now on. Commented Feb 16, 2011 at 21:15

2 Answers 2

1

Alright sorry, I thought you were looking for some complex code. Here is a simple example of exactly what you're looking for.

<html>
<head>
</head>
<body>
<script>
function doMath() {
    var totalparts = parseInt(document.getElementById('parts_input').value);
    var labor = parseInt(document.getElementById('labor_input').value);
    var misc = parseInt(document.getElementById('misc_input').value);
    var subtotal = totalparts + labor + misc;
    var tax = subtotal * .13;
    var total = subtotal + tax;

    document.getElementById('subtotal_input').value = subtotal;
    document.getElementById('tax_input').value = tax;
    document.getElementById('total_input').value = total;
}
</script>

<div>Total Parts: <input type="text" id="parts_input" value="1" readonly="true" /></div>
<div>Labor: <input type="text" id="labor_input" onBlur="doMath();" /></div>
<div>Misc: <input type="text" id="misc_input" onBlur="doMath();" /></div>
<div>Sub Total: <input type="text" id="subtotal_input" readonly="true" /></div>
<div>Tax: <input type="text" id="tax_input" readonly="true" /></div>
<div>Total: <input type="text" id="total_input" readonly="true" /></div>
</body>
</html>

Obviously this doesn't grab the dynamic value from a database. If you use PHP you can swap this line:

<div>Total Parts: <input type="text" id="parts_input" value="1" readonly="true" /></div>

for one like this:

<div>Total Parts: <input type="text" id="parts_input" value="<?PHP include('getTotalParts.php'); ?>" readonly="true" /></div>

Where the getTotalParts.php is a file you make to get your database information. It can simply grab the information and do a "echo $totalParts;"

Sign up to request clarification or add additional context in comments.

Comments

0

You can just use onblur (activated when a user leaves each of the input fields) to calcuate the fields.

...<input name="labour" id=total onblur="$('#total').val($('#sub').val() + $('#tax').va())">

You haven't provided enough information to comment on the "Total Parts" field.

2 Comments

Total parts is a number populated from Mysql, could you elaborate on your answer. Does this need to be in each input that needs to be calulated. I wanted all three to update once the labor field was filled and again if the misc field was filled.
No, it has to be in each field that is used as an input for other fields so that the calculated fields are updated when there is a change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.