473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Newb problem with checkboxes

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/javascript...oxes/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[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_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_typ es, 0)" checked>No Preference:
<br>
<input type=checkbox name="food_types" value="Mexican" onclick="checkChoice(document.survey_form.food_typ es, 1)"> Mexican:
<br>
<input type=checkbox name="food_types" value="Thai" onclick="checkChoice(document.survey_form.food_typ es, 2)"> Thai:
<br>
<input type=checkbox name="food_types" value="Unlisted Food" onclick="checkChoice(document.survey_form.food_typ es, 3)">Unlisted Food Type:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Plea se 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/javascript...oxes/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[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_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_typ es[], 0)" checked>No Preference:
<br>
<input type=checkbox name="food_types[]" value="Mexican" onclick="checkChoice(document.survey_form.food_typ es[], 1)"> Mexican:
<br>
<input type=checkbox name="food_types[]" value="Thai" onclick="checkChoice(document.survey_form.food_typ es[], 2)"> Thai:
<br>
<input type=checkbox name="food_types[]" value="Unlisted Food" onclick="checkChoice(document.survey_form.food_typ es[], 3)">Unlisted Food Type:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Plea se 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
Jul 23 '05 #1
9 1248
Lee
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

Jul 23 '05 #2
In article <db********@drn.newsguy.com>, Lee <RE**************@cox.net> wrote:
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
Jul 23 '05 #3
"claudel" <cl*****@bolt.sonic.net> wrote in message
news:db**********@bolt.sonic.net...
In article <db********@drn.newsguy.com>, Lee
<RE**************@cox.net> wrote:
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4
In article <cf**************@news2.mts.net>,
Grant Wagner <gw*****@agricoreunited.com> wrote:
"claudel" <cl*****@bolt.sonic.net> wrote in message
news:db**********@bolt.sonic.net...
In article <db********@drn.newsguy.com>, Lee
<RE**************@cox.net> wrote:
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


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
Jul 23 '05 #5
claudel wrote:
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

Jul 23 '05 #6
ASM
claudel wrote:

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[i].checked = false;
else // A checkbox other than "Any" selected.
if(field[i].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[i].checked = false;
else // A checkbox other than "Any" selected.
if(field[i].checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice(this,3)">
Unlisted Food Type

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
Janwillem Borleffs wrote:
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[i].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

Jul 23 '05 #8
In article <42***********************@news.euronet.nl>,
Janwillem Borleffs <jw@jwscripts.com> wrote:
claudel wrote:
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
Jul 23 '05 #9
In article <42***********************@news.wanadoo.fr>,
ASM <st*********************@wanadoo.fr.invalid> wrote:
claudel wrote:

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.element s['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[i].checked = false;
else // A checkbox other than "Any" selected.
if(field[i].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[i].checked = false;
else // A checkbox other than "Any" selected.
if(field[i].checked) field[0].checked = false;
}
------- that gives ----------
<input type="checkbox" name="<?php $food_types[] php?>"
onclick="checkChoice(this,3)">
Unlisted Food Type

--
Stephane Moriaux et son [moins] vieux Mac


Thanks much.
That helps...

Claude
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mellie | last post by:
Hi there, SQL 2000 database with ASP I've found many things "out there" regarding updating and deleting multiple records with checkboxes but I can't seem to find anything about adding them. ...
1
by: abs | last post by:
Hi. I've got such html source: <form name=my_form> <input type=checkbox class="a"><input type=checkbox class="b"><br> <input type=checkbox class="a"><input type=checkbox class="b"><br> <input...
4
by: Charles LePage | last post by:
Greetings! I'm attempting to write a form with checkboxes. This is a list of comic books, and what I need the form to do is produce a new page that only lists what the user has checked. And I'm...
0
by: Garet | last post by:
Hi Everyone, I'm having some trouble working with a TreeView with checkboxes. It is being used in a DotNet (1.1) Desktop Windows Application for file selection, together with a ListView. The...
1
by: CViniciusM | last post by:
Hello, I have used the code below (into the oncreate method) to get a treeview with checkboxes: HWND handle = TreeView1->Handle; value = GetWindowLong(handle, GWL_STYLE); if(value) { value =...
0
by: Xarky | last post by:
Hi, I am using a listview with checkboxes set as true. I need to retrieve all the rows of data that have the checkboxe set. Can someone tell me which properties do I have to handle to do that,...
1
by: dave | last post by:
I have a datagrid with checkboxes for each row. The user is able to select the ones he wants, then hit a "Save" button, which then loops through them all and insert the checked ones into a DB. ...
2
by: Carlos | last post by:
Hi all, I am looking into having a column in my datagrid with checkboxes and a button at the end to capture some kind of even command to take an action depending on the status of the checkbox....
4
by: ramapv | last post by:
can i highlight a checkbox from a group of checkbox with particular name which is given as a search key. I am having a list of checkboxes and i have to select some of them and form a group.but i'm...
2
by: AnaOlinto | last post by:
Hi, For the page I'm developing I need a DropDownList with CheckBoxes on it, so that the users can choose more than one option without having to use the Ctrl key (and it's more intuitive). Because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.