#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Florida
    Posts
    8
    Rep Power
    0

    PHP, forms, arrays and JavaScript


    I have a checkbox section to my HTML form. A user can select multiple checkboxes Each checkbox is named "cat[]" and has a different value, so as to allow PHP to take the values of any checked boxes and place them in an array, ie:

    <form name="frmSignUp">
    <input type="checkbox" name="cat[]" value="1">
    <input type="checkbox" name="cat[]" value="2">
    <input type="checkbox" name="cat[]" value="3">

    The problem comes when I try to use JavaScript to loop through this array and list out any checked boxes. I can do it fine in PHP, but no JavaScript.

    Here's how I am trying to do it now, and it isn't working:

    for (i = 0; i <document.frmSignUp.cat.length; i++)
    {
    // statements using cat[i]
    }

    JavaScript it seems doesn't like the fact that the form element name is cat[] - that is, JavaScript doesn't like the open bracket and close bracket characters in the name.

    Can somebody help out here?
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    try using "elements['cat[]'].length" instead of "cat.length", since there is no element in that form called "cat". the square brackets are part of the element's name, I think.
  4. #3

  5. Join Date
    Aug 2001
    Location
    NYC
    Posts
    57
    Rep Power
    14
    cat[] is not valid for javascript in terms of a checkbox name attribute.

    you'd have to use cat[1] cat [2] cat[3] and then cycle through all the cat[$i] variables using javascript.

    this works in php b/c php will interpret the variables sent to it differently.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Florida
    Posts
    8
    Rep Power
    0

    need more help, please


    Merkinmuffly: that didn't work. Any other ideas?

    Persaltier: Are you saying I can't name my checkboxes cat[]?
    and be able to refer to them in JavaScript?

    The goal is to use JavaScript to validate user input on the checkboxes and make sure that the user checks at least 3 of the checkboxes.

    -Todd
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Florida
    Posts
    8
    Rep Power
    0

    problem solved


    well, I solved my problem. I may be doing what was suggested above - at this point I've sort of lost track, but here's what I did:

    to loop through the array variable in JavaScript,
    where the array variable is from an HTML form
    and the array variable is named "cat[]"
    and the form is named "frmSignUp":

    var checkedCats = 0;
    for (i = 0; i < this.document.frmSignUp.elements['cat[]'].length; i++)
    { // statements involving
    this.document.frmSignUp.elements['cat[]'][i]
    }


    Best
    -Todd

IMN logo majestic logo threadwatch logo seochat tools logo