passing multiple checkbox values (with one Name value)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
k9kid
Forum Newbie
Posts: 3
Joined: Sat Nov 16, 2002 10:24 pm

passing multiple checkbox values (with one Name value)

Post by k9kid »

Hi... and thanks in advance for any help.

I am trying to use checkboxes from column items in my database table, then take the selections for use in my query. My problem is that the only passed variable is the last item in the series that has been chosen. How do you pass multiple selected values for the same "name" value (in separate checkboxed input fields) to the form handler (and how do you deal with them to separate them out??

the following snippets approximate what I have so far:

****From Form Input - these are generated dynamically from a MySQL table ****
<nobr><input type=checkbox name="state" value="CO">Colorado</nobr><br>
<nobr><input type=checkbox name="state" value="CT">Connecticut</nobr><br>
<nobr><input type=checkbox name="state" value="DC">District of Colombia</nobr><br>

****

****In Form handler****
$_POST['state'];
if (!is_array($state)) {
// action if not an array -- this is not the actual action I would use
echo "$state";
} else {
while(list($key2,$val2) = each($state)) {
// action if it is an array -- same deal -- would actually use values in a query
echo "$val2[$key2]";
}
}
****

$state appears to ONLY be a variable (not an array), even when more than one checkbox is chosen. I know that with the 'multiple' option in <Select> inputs that you need to place brackets [] after the name to have the multiple items passed through as an array, but what do I do to get an array from multiple checkboxes?? Or is there an alternative?
Thanks 8O
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

samething as the select box. add []'s at the end of the names
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it doesn't matter from what type of input-element values are passed to php. In fact php will not know it. Everything 'ships' as key/value-pair (either as part of the url which is is called GET-method or in the request body -called POST-method).
Think of the way php treats those key/value-pairs as of php-code.
  • html: <input type="text" name="key" value="value" />
  • request: key=value
  • php(old): $key = 'value';
  • php(new): e.g. $_POST['key']='value'
  • <input type="text" name="key[]" value="value1" />
    <input type="text" name="key[]" value="value2" />
  • key[]=value1&key[]=value2
  • $key[] = 'value1';
    $key[] = 'value2';
  • $_POST['key'][]='value1';
    $_POST['key'][]='value2';
  • <input type="text" name="key[firstDim][secondDim]" value="value" />
  • key[firstDim][secondDim]=value
  • $key['firstDim']['secondDim'] = 'value';
  • $_POST['key']['firstDim']['secondDim']='value';
  • <input type="text" name="key[]" value="text" />
    <input type="hidden" name="key[]" value="hidden" checked="true"/>
  • key[]=text&key[]=hidden
  • $key[]='text';
    $key[]='hidden';
  • $_POST['key'][]='text';
    $_POST['key'][]='hidden';
  • <input type="checkbox" name="key[]" value="chk1" />
    <input type="checkbox" name="key[]" value="chk2" />
    <input type="checkbox" name="key[]" value="chk3" />
    <input type="checkbox" name="key[]" value="chk4" />
  • if first and last are checked:
    key[]=chk1&key[]=chk4
  • $key[]='chk1';
    $key[]='chk4';
  • $_POST['key'][]='chk1';
    $_POST['key'][]='chk4';
but :!:
  • <input type="checkbox" name="key" value="chk1" />
    <input type="checkbox" name="key" value="chk4" />
  • key=chk1&key=chk4
  • $key='chk1';
    $key='chk4';
  • $_POST['key']='chk1';
    $_POST['key']='chk4';
  • the latter overrides the previous :?
for storing the values from an array to a single field in the database take a look e.g. at
http://www.php.net/manual/en/function.implode.php and http://www.php.net/manual/en/function.serialize.php
Nethus
Forum Newbie
Posts: 1
Joined: Wed Jan 15, 2003 9:29 am
Location: Programmer

Post by Nethus »

The problem is solved if you use brackets "[]" in the name of the checkboxes.

Ej.
In Form:
<input type="checkbox" name="idGrupos[]" value="9">
<input type="checkbox" name="idGrupos[]" value="8">
<input type="checkbox" name="idGrupos[]" value="7">

In request page:
echo $idGrupos[0] . "<br>";
echo $idGrupos[1] . "<br>";
echo $idGrupos[2] . "<br>";


It rules! :P
nu2php
Forum Newbie
Posts: 1
Joined: Thu Jul 10, 2003 1:12 pm

checkoxes

Post by nu2php »

What should I do if I need to use a javascript that selects / deselects all checkboxes that are being used in an html form that calls php. In other words javascript does not recognizes "[]" but php has to have it in order to get all values of selected checkboxes....

here is the code:
<SCRIPT LANGUAGE="JavaScript">
<!--
var checkflag = "false";
function checkall(field) {

if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;}
checkflag = "true";
}
else {
for (i = 0; i < field.length; i++) {
field.checked = false; }
checkflag = "false";
}
}
// End -->

<input type=checkbox value="check_all_box" onClick="checkall(this.form.mycheckbox)"> Select/Deselect All

<input type=checkbox name=mycheckbox value="1">
<input type=checkbox name=mycheckbox value="2">
<input type=checkbox name=mycheckbox value="3">
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

id's

Post by phpScott »

The best way I have found for using checkboxes or radio buttons or anything that is going to have the same name is to use name and id's in the html elements.

Code: Select all

<input type="checkbox" name="mycheckbox&#1111;]" id="mycheckbox" value="1">
<input type="checkbox" name="mycheckbox&#1111;]" id="mycheckbox" value="2">
<input type="checkbox" name="mycheckbox&#1111;]" id="mycheckbox" value="3">
then in your javascript code you can go things like

Code: Select all

someVal=document.getElementById('mycheckbox').value;
you get the idea the getElementById(); is very useful and is supported in all the browsers as far as I know.

phpScott
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

or php reading the vars

Code: Select all

<?php
foreach (array_keys($HTTP_POST_VARS) as $key) {
  echo "$HTTP_POST_VARS[$key]<br\>\n";
  }
?>
Post Reply