Unable to reference inputs when the name ends with '[]'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • inamul
    New Member
    • Sep 2007
    • 8

    Unable to reference inputs when the name ends with '[]'

    Hi Guys,

    I am am trying to validate multiple check box. It works fine when checkbox name is like chkSectionInter ested but when i add [] to the name chkSectionInter ested[], then i can validate any more.

    I need to keep [] so that i can save data using php collected from this page.

    If i remove [] from checkbox name then php only gets last selected value from the check box please help. I have been looking for two days but nothin seems to work.

    [CODE=javascript]
    function checkBox()
    {
    alert("Q1");

    // Validate Check boxs
    //-------------------------
    var filledIn = false;
    alert(document. forms[0].chkSectionInte rested.length);
    // Use the length property to iterate through each Checkbox
    // to determine if a selection has been made

    // ----- Area Of Interest

    for (var counter=0; counter<documen t.forms[0].chkSectionInte rested.length; counter++)
    {
    if (document.forms[0].chkSectionInte rested[counter].checked == true)
    {
    filledIn = true;

    //else{
    if (document.forms[0].chkSectionInte rested.checked == true)
    {
    filledIn = true;
    }
    }
    }
    if (filledIn == false)
    {
    alert('Please select your area of interest');
    document.forms[0].chkSectionInte rested[0].focus();
    return(false);
    }
    return (true);
    }
    [/CODE]
    [HTML]
    <input class="clsSecti onInterested" type="checkbox" name="chkSectio nInterested[]" value="Beauty" style="font-family: Arial; font-size: 10pt; border: 0px solid #C0C0C0; padding: 0">
    <input class="clsSecti onInterested" type="checkbox" name="chkSectio nInterested[]" value="Wellness " style="font-family: Arial; font-size: 10pt; border: 0px solid #C0C0C0; padding: 0">
    <input class="clsSecti onInterested" type="checkbox" name="chkSectio nInterested[]" value="Saloon" style="font-family: Arial; font-size: 10pt; border: 0px solid #C0C0C0; padding: 0">[/HTML]


    PHP Code

    [PHP]
    $strSectionInte rested="";
    $count=count($c hkSectionIntere sted);
    for($i=0;$i<$co unt;$i++){$strS ectionIntereste d="$strSectionI nterested$chkSe ctionInterested[$i], ";}[/PHP]

    ALL THE CODE WORKS FINE BUT DOES NOT VALIDTAE IF I REMOVE [ ] FROM CHECKBOX NAME AND IF I REMOVE [ ] THEN PHP GET ONLY THE LAST SELECTED VALUE FROM THE CHECK BOX AND ABOVE ALL I HAVE TO VALIDATE SO THAT USER ATLEAST SELECT 1 OPTION.
    Last edited by acoder; Sep 5 '07, 01:55 PM. Reason: Added language to code tag
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Use document.getEle mentsByName():
    [CODE=javascript]var checkboxes = document.getEle mentsByName("ch kSectionInteres ted[]");[/CODE]

    Comment

    • inamul
      New Member
      • Sep 2007
      • 8

      #3
      Originally posted by acoder
      Welcome to TSDN!

      Use document.getEle mentsByName():
      [CODE=javascript]var checkboxes = document.getEle mentsByName("ch kSectionInteres ted[]");[/CODE]

      Thanks for your prompt reply i tried but i still get error may be i am doing some thing wrong.

      could you please implement it in the same code so that i have complete example.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Try this: [CODE=javascript]
        var checkboxes = document.getEle mentsByName("ch kSectionInteres ted[]");
        for (var counter=0; counter<checkbo xes.length; counter++)
        {
        if (checkboxes[counter].checked == true)
        {
        filledIn = true;
        }
        }
        if (filledIn == false)
        {
        alert('Please select your area of interest');
        return false;
        }
        return true;[/CODE]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Changed thread title to better describe the problem.

          Comment

          • inamul
            New Member
            • Sep 2007
            • 8

            #6
            Originally posted by acoder
            Try this: [CODE=javascript]
            var checkboxes = document.getEle mentsByName("ch kSectionInteres ted[]");
            for (var counter=0; counter<checkbo xes.length; counter++)
            {
            if (checkboxes[counter].checked == true)
            {
            filledIn = true;
            }
            }
            if (filledIn == false)
            {
            alert('Please select your area of interest');
            return false;
            }
            return true;[/CODE]
            Thanks for your help It works.. This really is an excellent forum.

            Thanks again
            Last edited by acoder; Sep 6 '07, 08:56 AM. Reason: Fixed quote tag

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Glad you got it working and you're welcome. Post again anytime should you need help.

              Comment

              Working...