#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    12

    PHP and JavaScript Dynamic Checkbox Problem


    I have two sets of dynamic checkboxes in PHP which are submited by one form and I was wondering if there is a way that I can validate them with Javascript functions using only that one form???

    The first set of checkboxes includes:

    PHP Code:
    echo "<input type=\"checkbox\" name=\"Courses[]\" value=\"$rows[Crskey]\">\r\n"
    The second set of checkboxes is:

    PHP Code:
    echo "<input type=\"checkbox\" name=\"Employee[]\" value=\"$row[Empkey]\">\r\n"
    And my Form is:

    <form name="form" action="scheduledemp.php" method="POST" onSubmit='return checkform(form);'>

    Thanks in advance for any assistance. Hope that this makes sense.
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    14
    what do you mean by "validate"? if they simply need to feel validated, i can give you the number of a good psychiatrist.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    12
    If validation is unclear let me say it differently.....I need to check and see that at least one of the checkboxes is checked on each one of those sets.
  6. #4
  7. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    14
    aha. that's much clearer.

    this is really more of a javascript question so you might get some blockheads yelling at you to move the thread to a different forum. just ignore them.

    since you named your checkbox inputs like "Courses[]", you created a javascript array of checkbox elements. you can loop over it like any array, almost. the only difference being, to reference it i think you have to talk about form.elements['Courses[]'].
    Code:
    function checkform(form)
    {
        for (var i=0; i<form.elements['Courses[]'].length; i++)
            if (form.elements['Courses[]'][i].checked)
                // found a Courses checkbox that was checked
    }
    hth

    ---------------------
    you might be able to replace "form.elements['Courses[]']" with "form.elements.Courses". don't quote me on that, tho.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    12
    Tried it but still doesn't work. I get a syntax error.
  10. #6
  11. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    14
    1. try making the checkform() function take no parameters, then change all references to "form" inside that function to "document.form".

    2. if that does nothing, post your checkform() function here.

IMN logo majestic logo threadwatch logo seochat tools logo