473,407 Members | 2,359 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,407 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 21 '05 #1
3 1808
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 21 '05 #2
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 21 '05 #3
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 21 '05 #4

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.