SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to SUM fields in form not database

    I need to be able to have a total form field that adds up amounts entered in 3 other form fields above it. The idea is that people enter haw many cars, vans etc that they have in a fleet and the total of all vehicles displays in the Total box and then records it to the databse on form submital.

    I have looked through the forum and all the questions I can see refer to the sum of specific fields or columns in a DB table but I need the sum to occur before the information is submitted to the table.

    I initially tried:

    $fleet_total = $fleet_cars + $fleet_lcv + $fleet_vans;

    thinking that this was the simplest way to do it and using the result of the variable $fleet_total in the form field but it just remains at zero.

    Can anyone help please?

    Thanks

  2. #2
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use some javascript if you want it to be client side, else,

    PHP Code:
    $fleet_total = (int) ($_POST['fleet_cars'] + $_POST['fleet_lvc'] + $_POST['fleet_vans']); 
    Mike
    It's not who I am underneath, but what I do that defines me.

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    You would need to use Javascriptfor this, but I'm no expert so try the JS forums

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Tacoma, WA
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The line you have won't work cause for one you are mixing php with javascript and the other reason is if you were doing that in JavaScript I believe you would be concatinating strings. Don't quote me on that.

    $fleet_total = $fleet_cars + $fleet_lcv + $fleet_vans;

    What you will need to do is use the Document Object Model and create a function that grabs those values, adds them up, then inserts them into a form field using JavaScript.

    - Tony


Bookmarks

Posting Permissions

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