Update Cost Field

Hi,

I have a basic form that has 6 check boxes with costs associated with each of them.

What I need to do is add up the associated cost if the check box is clicked on and place it in a Total Field. If two check boxes are clicked then it needs to add them up and update the Total Costs field.

Not sure what language to do this in. The current page is PHP.

Thanks
Grant
grantwatsonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
b0lsc0ttConnect With a Mentor IT ManagerCommented:
This is javascript then.  You can make a function like the one below.  It would go in the head section of the page in between script tags.

  function add2Total(box) {
    var total = document.getElementById("totalfield");
    if (box.checked) {
      total.value = total.value + box.value;
    } else {
      total.value = total.value - box.value;
    }
  }

Since you didn't provide specifics on the form and fields you may need to make some changes.  The function assumes the total cost field is a text input box.  It also assumes that the id attribute is totalfield.  The function is set up to be called as an onclick event from each checkbox tag.  The value tag has the amount you want added or subtracted when the box is checked or unchecked.  An example is below.

  <input type="checkbox" name="prod1" value="10" onclick="add2Total(this);">

This should at least give you an idea of how to do it.  If you need specifics or help putting it in your page I will need that part of your html.

bol
0
 
b0lsc0ttIT ManagerCommented:
grantwatson,

Do you want this to occur when the form is submitted?  If not, when?

What is some of the code?  Depending on when you want this to happen you would use PHP or Javascript.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
grantwatsonAuthor Commented:
It needs to happen when a check box is clicked on. So say i have a list of products. When one of the products is selected by clicking the check box the Total Cost field updates with the cost.

thanks
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
grantwatsonAuthor Commented:
Thanks, i'll give it ago.
0
 
grantwatsonAuthor Commented:
Hi,

Got it working sort of... Its adding the values as text values in stead of numbers.

e.g. 151540

instead of getting 70.

Grant
0
 
b0lsc0ttIT ManagerCommented:
It is treating the values as strings.  Use parseFloat() to convert them to numbers.

  function add2Total(box) {
    var total = document.getElementById("totalfield");
    if (box.checked) {
      total.value = parseFloat(total.value) + parseFloat(box.value);
    } else {
      total.value = parseFloat(total.value) - parseFloat(box.value);
    }
  }
0
 
grantwatsonAuthor Commented:
Thanks for your quick response and solution.

Works great
0
 
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad that I could help.  Thank you for the grade, the points and the fun questions.

bol
0
 
Michel PlungjanIT ExpertCommented:
Or
function add2Total(box) {
    var totalFld = box.form.totalfield;
    totalFld.value = parseFloat(totalFld.value) + (parseFloat(box.value) *(box.checked)?-1:1)
  }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.