Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help - Validate CheckBox[] Array

    How can I validate a checkbox array using javascript?

    I am using HTML and PHP to parse the data.

    HTML Code:

    ***
    <input type="checkbox" name="game[]" id="game" value="game" />

    **


    PHP Code:


    ***
    $state = Trim(stripslashes($_POST['game']));


    if(isset($_POST['game']) && is_array($_POST['game']))
    {
    foreach($_POST['game'] as $key=>$val)
    {
    switch($val)
    {
    case 'Game Type':
    $email = "myemailaddress";
    $Body .= <<<EOF
    ***

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    What is there to validate? I'm not sure what you'd be checking-- if it's on or off? if (document.form1.checkBox.checked) {alert("yeah it's checked alright");}

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    If you want to check a group of checkboxes. You will have to loop through the input tags to see if the checkboxes are checked or not.

    If you are populating the checkboxes with a PHP array, be sure to give the same value to the name as you do for the id.
    Having the same value doesn't make much sense either:
    Code:
    <input type="checkbox" name="game[]" id="game[]" value="game[]" />
    Here's an example:
    Code:
    <script type="text/javascript">
    function chkBox() {
    var chkedBoxes = [], i = 0, inputs = document.getElementsByTagName('input');
     while (i<inputs.length) {
      if (inputs[i].type == 'checkbox' && inputs[i].checked) {
       // it's checked
        chkedBoxes.push(inputs[i].value);
        inputs[i].checked = false;
      }
      i++;
     }
    alert(chkedBoxes.length + ' item(s) were selected\n' + chkedBoxes);
    }
    </script>
    
    
    <form id="gameform" name="gameform" method="post" action="" enctype="multipart/form-data">
    <input type="checkbox" name="game1" id="game1" value="game1" />
    <input type="checkbox" name="game2" id="game2" value="game2" />
    <input type="checkbox" name="game3" id="game3" value="game3" />
    <input type="checkbox" name="game4" id="game4" value="game4" />
    <input type="button" onclick="chkBox()" value="Run Check">
    </form>

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm sorry. The values I entered are fictitious :

    <input type="checkbox" name="game[]" id="game[]" value="game[]" />

    This is a true name - name="game[]"

    Thanks for your suggestion, but the script/html doesn’t work correctly with my PHP code.

    name="game[]" is an array. PHP will only display the html data (array) if it's checked. How can I incorporate name="game[]" into your script/html?


  •  

    Posting Permissions

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