SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,449
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Looping through all checkboxes in a form

    I have a form that I need to auto submit when a user has clicked three of the possible 'x' number of checkboxes. I know I will use the onclick attribute to call a function to check to see how many are filled in, but I'm unclear as to the syntax to refer to the checkboxes in my attempt to count how many are checked. I guess I should post the code for the form for reference:

    Code:
    <form method="post" action="/calc.php" name="processform">
    <input type="hidden" name="compare" value="true" />
    Enter your average ticket price: <input type="text" name="aveticket" width="10" />
    Enter your average number of transactions per month <input type="text" name="numtickets" width="10" />
    
    <input type="checkbox" name="processors[]" value="1" onclick="someFunction">JNA Web Design</input>
    <input type="checkbox" name="processors[]" value="2" onclick="someFunction">Paypal</input>
    <input type="checkbox" name="processors[]" value="3" onclick="someFunction">WorldPay</input>
    <input type="checkbox" name="processors[]" value="4" onclick="someFunction">2CheckOut</input>
    <input type="checkbox" name="processors[]" value="5" onclick="someFunction">PaySystems Plan A</input>
    <input type="checkbox" name="processors[]" value="6" onclick="someFunction">PaySystems Plan B</input>
    
    <input type="submit" value="Compare Programs" />
    </form>

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe something like this:
    <html>
    <head>
    <title>test</title>
    <script language="javascript">
    function someFunction(frmobj)
    {
    count=0
    len=frmobj.length
    for(i=0;i<len;i++)
    {
    if(frmobj.elements[i].type=="checkbox")
    {
    if(frmobj.elements[i].checked)
    {
    count++
    }
    }
    }
    if(count>2)
    {document.myform.submit()}
    }

    </script>
    </head>
    <body>
    <form name="myform" method="post" action="page1.html">
    <input type="text" size="10" name="txt"><br>
    <input type="checkbox" name="ckbox" onClick="someFunction(document.myform)"><br>
    <input type="checkbox" name="ckbox" onClick="someFunction(document.myform)"><br>
    <input type="checkbox" name="ckbox" onClick="someFunction(document.myform)"><br>
    <input type="checkbox" name="ckbox" onClick="someFunction(document.myform)"><br>
    </form>
    </body>
    </html>


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
  •