Yet another newb checkbox question

C

claudel

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.javascript.

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="JavaScript">
<!-- Original: Scott Waichler -->

<!-- Shamelessly borrowed from http://www.jsmadeeasy.com/javascripts/Forms/Controlled Boxes/index.htm -->

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

</head>

<body>

<form name=survey_form>

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

<input type=checkbox name="food_types" value="No Preference" onclick="checkChoice(document.survey_form.food_types, 0)" checked>No Preference:
<br>
<input type=checkbox name="food_types" value="Mexican" onclick="checkChoice(document.survey_form.food_types, 1)"> Mexican:
<br>
<input type=checkbox name="food_types" value="Thai" onclick="checkChoice(document.survey_form.food_types, 2)"> Thai:
<br>
<input type=checkbox name="food_types" value="Unlisted Food" onclick="checkChoice(document.survey_form.food_types, 3)">Unlisted Food Type:<br>&nbsp;&nbsp;&nbsp;&nbsp;&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="JavaScript">
<!-- Original: Scott Waichler -->

<!-- Shamelessly borrowed from http://www.jsmadeeasy.com/javascripts/Forms/Controlled Boxes/index.htm -->

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

</head>

<body>

<form name=survey_form>

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

<input type=checkbox name="food_types[]" value="No Preference" onclick="checkChoice(document.survey_form.food_types[], 0)" checked>No Preference:
<br>
<input type=checkbox name="food_types[]" value="Mexican" onclick="checkChoice(document.survey_form.food_types[], 1)"> Mexican:
<br>
<input type=checkbox name="food_types[]" value="Thai" onclick="checkChoice(document.survey_form.food_types[], 2)"> Thai:
<br>
<input type=checkbox name="food_types[]" value="Unlisted Food" onclick="checkChoice(document.survey_form.food_types[], 3)">Unlisted Food Type:<br>&nbsp;&nbsp;&nbsp;&nbsp;&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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top