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

    Join Date
    Dec 2000
    Posts
    23
    Rep Power
    0

    Unhappy


    hi,

    while i trying to figure out how to use javascript to
    ensure user at least check one of the checkbox upon submit,
    i found this script from internet:

    <HEAD>

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Original: Alan Gruskoff (alan@performantsystems.com) -->
    <!-- Web Site: http://www.performantsystems.com/ -->

    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! http://javascript.internet.com -->

    <!-- Begin
    function anyCheck(form) {
    var total = 0;
    var max = form.ckbox.length;
    for (var idx = 0; idx < max; idx++) {
    if (eval("document.playlist.ckbox[" + idx + "].checked") == true) {
    total += 1;
    }
    }
    alert("You selected " + total + " boxes.");
    }
    // End -->
    </script>

    </HEAD>

    <!-- STEP TWO: Copy this code into the BODY of your HTML document -->

    <BODY>

    <form method="post" name=playlist>
    1<input type=checkbox name=ckbox>
    <br>2<input type=checkbox name=ckbox>
    <br>3<input type=checkbox name=ckbox>
    <br>4<input type=checkbox name=ckbox>
    <br>5<input type=checkbox name=ckbox>
    <br>6<input type=checkbox name=ckbox>
    <br>7<input type=checkbox name=ckbox>
    <br>8<input type=checkbox name=ckbox>
    <br>9<input type=checkbox name=ckbox>
    <p><input type=button value="Count Checkboxes" onClick="anyCheck(this.form)">
    </form>

    <p><center>
    <font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
    by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
    </center><p>

    while it would be nice if i can implement in my php code
    so that if total is 0, then the program will pop out a
    message to make sure user at least check one of the checkbox

    BUT, in php, we use chkbox[] as our variable name, cos
    the checkbox is dynamic. but when i try to access lenght
    of the checkbox using :

    maxchk = form.chkbox[].length;

    or

    maxchk = form.chkbox.length;

    the script just won't execute.

    do anyone encounter this problem before?

    thanks,
    melv


  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,648
    Rep Power
    4493
    Hmmm...maybe you'll just have to do it on the server side?

    if(count($checkbox) == 0)
    {
    //redisplay form with error message
    }
    else
    {
    //process form
    }

    ---John Holmes...
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo