SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Subtract Drop-Down Box Value

    Hi Guys,

    Using the following code, how do I subtract a Drop-Down Box value from another Drop-Down Box value?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    </head>
    
    <body>
    <select name="select">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
    <select name="select2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
    </body>
    </html>
    TicTac

  2. #2
    SitePoint Zealot chrissie76's Avatar
    Join Date
    Sep 2007
    Location
    plymouth, devon, uk
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want to do this before the form is submitted you need to be familiar with JavaScript, if not then you need to deceide whether your server parses php, or asp or whatever. it should be very very easy to do.

  3. #3
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Subtract Drop-Down Box Value

    Hi chrissie76,

    I have some experience with JavaScript, but not in terms of maths - Subtracting numbers in an object (e.g. Drop-Down Box).
    How would I go about doing this?

    TicTac

  4. #4
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think TicTac wants to be able to do this in JavaScript considering he posted his question in this thread. This ones on me, but if you really don't know the language you need to read a tutorial or better yet a book. Danny Goodman's JavaScript Bible is a good one.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function calc()
    {
    var select1=document.getElementsByTagName("select")[0]
    var select2=document.getElementsByTagName("select")[1]
    var si1=select1.options.selectedIndex
    var si2=select2.options.selectedIndex
    var result=parseInt(select1.options[si1].value)+parseInt(select1.options[si2].value)
    document.getElementById("resultbox").value=result
    }
    </script>
    </head>
    
    <body>
    <form>
    <select name="select">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
    <select name="select2">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
    </select>
    <input id="resultbox" size="2" type="text" /><br /><br />
    <input type="button" value="calculate" onclick="calc()" />
    </form>
    </body>
    </html>
    Just a few things to note:
    1. You should get in the habbit of using id in place of name.
    2. To find out which box is currently selected use the selectedIndex property.
    3. To convert strings to numbers use parseInt.

  5. #5
    SitePoint Zealot chrissie76's Avatar
    Join Date
    Sep 2007
    Location
    plymouth, devon, uk
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes you are quite right, he did post it in javascript! oh dear... well thanks for pointing it out anyway.. :S

  6. #6
    Non-Member
    Join Date
    Aug 2007
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Subtract Drop-Down Box Value

    Many Thanks

  7. #7
    Non-Member
    Join Date
    Sep 2007
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    chrissie76,

    Just out of interest, how would you carry this problem out using PHP?

    TheBIGCube

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Considering the original post has no SUBMIT facility this might be difficult.

    Assuming there is a SUBMIT and the method="GET" ...

    PHP Code:
    if (isset ($_GET['submit_button'] {
        
    $sel1 $_GET['select'];
        
    $sel2 $_GET['select2'];
        
    $result $sel1 $sel2;
        echo 
    $result;

    PS - note that the question was to SUBTRACT one from the other, not ADD them.
    Ian Anderson
    www.siteguru.co.uk


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
  •