Solved

Update Cost Field

Posted on 2006-11-11
9
181 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:grantwatson
  • 4
  • 4
9 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17923406
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
 

Author Comment

by:grantwatson
ID: 17923568
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
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 17923657
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:grantwatson
ID: 17923659
Thanks, i'll give it ago.
0
 

Author Comment

by:grantwatson
ID: 17923705
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17923739
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
 

Author Comment

by:grantwatson
ID: 17923754
Thanks for your quick response and solution.

Works great
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17923760
Your welcome!  I'm glad that I could help.  Thank you for the grade, the points and the fun questions.

bol
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17925324
Or
function add2Total(box) {
    var totalFld = box.form.totalfield;
    totalFld.value = parseFloat(totalFld.value) + (parseFloat(box.value) *(box.checked)?-1:1)
  }
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing Flash from Website 6 65
Finding the platform that was used to build a website 4 50
Getting attribute Values using xslt 4 47
jQuery Date picker not working 16 131
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question