Yet another newb checkbox question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • claudel

    Yet another newb checkbox question

    Hi

    I have a newb PHP/Javascript question regarding checkbox processing
    I'm not sure which area it falls into so I crossposted to comp.lang.php
    and comp.lang.javas cript.

    I'm trying to construct a checkbox array in a survey form where one
    of the choices is "No Preference" which is checked by default.

    If the victim chooses other than "No Preference", I'd like to uncheck
    the "No Preferences" box and submit the other choices to the rest of the
    form as an array.

    I have most of it worked out, but I'm stuck on an apparent disconnect
    between php and javascript.

    When I use this code

    _______________ _____example 1 _______________ _______________ ________

    <head>

    <SCRIPT LANGUAGE="JavaS cript">
    <!-- Original: Scott Waichler -->

    <!-- Shamelessly borrowed from http://www.jsmadeeasy.com/javascript...oxes/index.htm -->

    <!-- Begin
    function checkChoice(fie ld, i) {
    if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
    for (i = 1; i < field.length; i++)
    field[i].checked = false;
    }
    }
    else { // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
    field[0].checked = false;
    }
    }
    }
    // End -->
    </script>

    </head>

    <body>

    <form name=survey_for m>

    <table>
    <tbody>
    <tr>
    <td>
    What is your favorite ethnic food type?&nbsp;&nbs p;<br>
    Check all that apply:&nbsp;&nb sp;<br>
    </td>
    <td>

    <input type=checkbox name="food_type s" value="No Preference" onclick="checkC hoice(document. survey_form.foo d_types, 0)" checked>No Preference:
    <br>
    <input type=checkbox name="food_type s" value="Mexican" onclick="checkC hoice(document. survey_form.foo d_types, 1)"> Mexican:
    <br>
    <input type=checkbox name="food_type s" value="Thai" onclick="checkC hoice(document. survey_form.foo d_types, 2)"> Thai:
    <br>
    <input type=checkbox name="food_type s" value="Unlisted Food" onclick="checkC hoice(document. survey_form.foo d_types, 3)">Unlisted Food Type:<br>&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; (Please describe below)
    </td>

    </td>
    </tr>
    </tbody>
    </table>


    </body>
    </form>
    </center>

    _______________ _____ end example 1______________ _______________ ______________

    the checkboxes act as I would like. The "No Preference" choice is deselected
    when another choice is made. Great.

    The problem is that php would like to see the selected choices referenced
    as "food_types[]" in order to process them as an array. This behavior
    is apparently necessary in order to stuff the selections into a database
    and for other uses.

    This code:

    _______________ _____ example 2 _______________ _______________ ________

    <head>

    <SCRIPT LANGUAGE="JavaS cript">
    <!-- Original: Scott Waichler -->

    <!-- Shamelessly borrowed from http://www.jsmadeeasy.com/javascript...oxes/index.htm -->

    <!-- Begin
    function checkChoice(fie ld, i) {
    if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
    for (i = 1; i < field.length; i++)
    field[i].checked = false;
    }
    }
    else { // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
    field[0].checked = false;
    }
    }
    }
    // End -->
    </script>

    </head>

    <body>

    <form name=survey_for m>

    <table>
    <tbody>
    <tr>
    <td>
    What is your favorite ethnic food type?&nbsp;&nbs p;<br>
    Check all that apply:&nbsp;&nb sp;<br>
    </td>
    <td>

    <input type=checkbox name="food_type s[]" value="No Preference" onclick="checkC hoice(document. survey_form.foo d_types[], 0)" checked>No Preference:
    <br>
    <input type=checkbox name="food_type s[]" value="Mexican" onclick="checkC hoice(document. survey_form.foo d_types[], 1)"> Mexican:
    <br>
    <input type=checkbox name="food_type s[]" value="Thai" onclick="checkC hoice(document. survey_form.foo d_types[], 2)"> Thai:
    <br>
    <input type=checkbox name="food_type s[]" value="Unlisted Food" onclick="checkC hoice(document. survey_form.foo d_types[], 3)">Unlisted Food Type:<br>&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; (Please describe below)
    </td>

    </td>
    </tr>
    </tbody>
    </table>


    </body>
    </form>
    </center>

    _______________ _______ end example 2 _______________ _______________ ________

    correctly populates the array and everything is fine except for the javascript
    part of the code that deselects "No Preference" when another selection is made
    no longer works.

    I need to know how I can change either the javascript code to work with the []
    in the php input statements or some other way to create an array with the choices
    that doesn't require the [] to be functional.

    I've looked around for a viable solution, but haven't found anything that
    seems that it will bridge this disconnect.

    Any other suggestions as to how I can do what I'm trying to do are also
    extremely appreciated.


    Thanks


    Claude
Working...