Newb problem with checkboxes

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 or pointers to
an explanation are also extremely appreciated.


Thanks


Claude
 
C

claudel

claudel said:
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.

[ nicely documented problem deleted ]

See this FAQ entry:
http://www.jibbering.com/faq/#FAQ4_25

Thanks Much

It seems a bit odd that a legal construct "[]" in php is
considered to be "illegal" in javascript, but if that's the
worst conflict I ever run into I'll be happy

Claude
 
G

Grant Wagner

claudel said:
Lee said:
claudel said:
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.

[ nicely documented problem deleted ]

See this FAQ entry:
http://www.jibbering.com/faq/#FAQ4_25

Thanks Much

It seems a bit odd that a legal construct "[]" in php is
considered to be "illegal" in javascript, but if that's the
worst conflict I ever run into I'll be happy

It's the other way around.

It seems a bit odd PHP would choose to use characters known to be
illegal within ID attributes in the standard (x)HTML doctypes and
javascript Identifiers as attributes in those contexts.

PHP came after HTML and JavaScript, it should not require the use of
characters previously documented as illegal in ID attributes.
 
C

claudel

claudel said:
Lee said:
claudel said:

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.

[ nicely documented problem deleted ]

See this FAQ entry:
http://www.jibbering.com/faq/#FAQ4_25

Thanks Much

It seems a bit odd that a legal construct "[]" in php is
considered to be "illegal" in javascript, but if that's the
worst conflict I ever run into I'll be happy

It's the other way around.

It seems a bit odd PHP would choose to use characters known to be
illegal within ID attributes in the standard (x)HTML doctypes and
javascript Identifiers as attributes in those contexts.

PHP came after HTML and JavaScript, it should not require the use of
characters previously documented as illegal in ID attributes.

Thanks for the clarification.

In any case, it seems a bit silly that software components that
theoretically need to co-operate do not. Especially regarding
basic constructs such as arrays...

Claude
 
J

Janwillem Borleffs

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

Then you have missed out on the JS Faq, because it is in there:

Instead of: document.survey_form.food_types[]
Do: document.survey_form.elements['food_types[]']

Or better yet:

document.forms['survey_form'].elements['food_types[]']

An alternative would be to enable the always_populate_raw_post_data
directive in your php.ini file, which enables you to grasp the element's
values through the $HTTP_RAW_POST_DATA variable without the need of using
the brackets.


JW
 
A

ASM

claudel said:
Thanks for the clarification.

In any case, it seems a bit silly that software components that
theoretically need to co-operate do not. Especially regarding
basic constructs such as arrays...

JS arrays and PHP arrays aren't similare

is it something like this below you try to do ?

<input type="checkbox" name="<?php echo $food_types[] php?>"
onclick="checkChoice(document.survey_form.elements['food_types[]'],3)">
Unlisted Food Type

to get an element nammed foo[]
->
document.forms['survey_form'].elements['foo[]']


----- with function this way ----
function checkChoice(field, i) {
field = document.forms['survey_form'].elements[field];
if(i == 0) // "All" checkbox selected.
if(field[0].checked)
for (i = 1; i < field.length; i++) field.checked = false;
else // A checkbox other than "Any" selected.
if(field.checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice('food_types[]',3)">
Unlisted Food Type



----- with function this way ----
function checkChoice(field, i) {
field = document.forms['survey_form'].elements[field.name];
if(i == 0) // "All" checkbox selected.
if(field[0].checked)
for (i = 1; i < field.length; i++) field.checked = false;
else // A checkbox other than "Any" selected.
if(field.checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice(this,3)">
Unlisted Food Type
 
J

Janwillem Borleffs

Janwillem said:
Or better yet:

document.forms['survey_form'].elements['food_types[]']

Or with a minor modification to the JS and the function call only:

<script type="text/javascript">
function checkChoice(obj, first) {
var elements = obj.form.elements[obj.name];
elements[0].checked = obj.checked && first;

if (first) {
for (var i = 1; i < elements.length; i++) {
elements.checked = false;
}
}
}
</script>
....
<input type=checkbox name="food_types[]" value="No Preference"
onclick="checkChoice(this, 1)" checked>No Preference:
<br>
<input type=checkbox name="food_types[]" value="Mexican"
onclick="checkChoice(this)"> Mexican:
<br>
<input type=checkbox name="food_types[]" value="Thai"
onclick="checkChoice(this)"> Thai:
<br>
<input type=checkbox name="food_types[]" value="Unlisted Food"
onclick="checkChoice(this)">Unlisted Food


JW
 
C

claudel

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

Then you have missed out on the JS Faq, because it is in there:

Instead of: document.survey_form.food_types[]
Do: document.survey_form.elements['food_types[]']

Or better yet:

document.forms['survey_form'].elements['food_types[]']

An alternative would be to enable the always_populate_raw_post_data
directive in your php.ini file, which enables you to grasp the element's
values through the $HTTP_RAW_POST_DATA variable without the need of using
the brackets.


JW

Thanks. I'm just getting started with JS...


Claude
 
C

claudel

claudel said:
Thanks for the clarification.

In any case, it seems a bit silly that software components that
theoretically need to co-operate do not. Especially regarding
basic constructs such as arrays...

JS arrays and PHP arrays aren't similare

is it something like this below you try to do ?

<input type="checkbox" name="<?php echo $food_types[] php?>"
onclick="checkChoice(document.survey_form.elements['food_types[]'],3)">
Unlisted Food Type

to get an element nammed foo[]
->
document.forms['survey_form'].elements['foo[]']


----- with function this way ----
function checkChoice(field, i) {
field = document.forms['survey_form'].elements[field];
if(i == 0) // "All" checkbox selected.
if(field[0].checked)
for (i = 1; i < field.length; i++) field.checked = false;
else // A checkbox other than "Any" selected.
if(field.checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice('food_types[]',3)">
Unlisted Food Type



----- with function this way ----
function checkChoice(field, i) {
field = document.forms['survey_form'].elements[field.name];
if(i == 0) // "All" checkbox selected.
if(field[0].checked)
for (i = 1; i < field.length; i++) field.checked = false;
else // A checkbox other than "Any" selected.
if(field.checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice(this,3)">
Unlisted Food Type


Thanks much.


That helps...

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top