SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array of Checkboxes Problem

    I'm having problems with checkboxes in both Javascript and PHP.

    What I have is a basic form with an array of check boxes where the user can check off the relevant options - the restriction is that they must select at least one checkbox from the list before proceeding.

    I therefore use javascript to make sure they've checked at least one box.

    And then on the next page, I have it store what boxes they checked into my database.

    Here's a chunk of the form code:

    PHP Code:
    <input name="box[]" type="checkbox" id="box[]" value="1">
    <
    input name="box[]" type="checkbox" id="box[]" value="2">
    <
    input name="box[]" type="checkbox" id="box[]" value="3">

    <
    input type="submit" value="Next" onClick="return checkForm(this.form);"
    Note that I want to use "box[]" instead of "box1", "box2", etc so that it will become an array on the next page for PHP to process.

    The whole problem is that when I try and detect in my "checkForm" javascript function if at least one box has been checked, I get these stupid Invalid Object errors.

    In other words, it's not properly accessing these check boxes.
    I know how to see if a box has been checked when they aren't in the array form like above, but I need them to be in that array form for the PHP processing.

    Does anyone know the javascript code to go through the boxes and detect if at least one has been checked?

    I've tried javascript code like:

    form.box[0].value

    But that gives me an object error.
    Thanks if you can help.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If nothing else works, you can try this:
    Code:
    var at_least_one_checked = false;
    for(var i = 0; i < frm.elements.length; i++)
    {
    	var elem = frm.elements[i];
    	if((elem.name.substr(0, 3) == 'box') && (elem.checked))
    	{
    		at_least_one_checked = true;
    		break;
    	}
    }
    frm == the value of this.form sent from the submit button

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have removed your other thread, please do not cross-post

    Why can't you just make it easier and have one box checked so one is selected whether the user alters it or not?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Enthusiast Analyser's Avatar
    Join Date
    Oct 2002
    Location
    Delft, The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not name your boxes this way:
    PHP Code:
    <input name="box[1]" type="checkbox" id="box[1]" value="1">
    <
    input name="box[2]" type="checkbox" id="box[2]" value="2">
    <
    input name="box[3]" type="checkbox" id="box[3]" value="3"
    Then you can use something like
    Code:
    // returns 1 if at least one box was checked, 0 if not.
    function check_boxes () {
        var i;
        for (i = 1; i <= numBoxes; i++) {
            var boxName = 'box['+i+']';
            if (document.form.elements[boxName].checked) {
                return 1;
            }
        }
        return 0;
    }
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/


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
  •