note 39518 deleted from language.variables.external by philip
| From: | philip@php.net | Date: | Thu, 12 Feb 2004 21:55:37 +0000 |
| Subject: | note 39518 deleted from language.variables.external by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65067@lists.php.net to get a copy of this message | ||
Note Submitter: Juan Prada [ jp at silueth.com ]
----
About passing a list of dinamic checkboxes, the problem is that if checked they will appear in the
parameter line in the format cbx_name[]=value, but if don't, they wont appear, so... ¿how to
parse the list of cbx_name[], how to know the index of each one?
Example:
You have a list of 3 dinamically created check boxes named as 'cbx_foo[]'
<?php
echo '
<input type="checkbox" name="cbx_foo[]" value="1">
<input type="checkbox" name="cbx_foo[]" value="1">
<input type="checkbox" name="cbx_foo[]" value="1">';
?>
When submittin' the form, if all 3 are checked they will appear as
..&cbx_foo[]=1&cbx_foo[]=1&cbx_foo[]=1
But, if the second one, for example, is not checked, will appear as:
..&cbx_foo[]=1&cbx_foo[]=1
So, how do we know that the second one in the param is in reallity referred to the third one?
First step, we have to identify each checkbox using the value atribute, so when 'painting'
then dinamic checkboxes, we have to set an index to this value.
They will look as this:
<?php
echo '
<input type="checkbox" name="cbx_foo[]" value="0">
<input type="checkbox" name="cbx_foo[]" value="1">
<input type="checkbox" name="cbx_foo[]" value="2">';
?>
So, when submining the form with the 1st and 3rd checked and not the 2nd, the param url string will
look:
..&cbx_foo[]=0&cbx_foo[]=2
Now we have to parse this values to our array of values, setting true the indexes that are marked
and false the ones who not.
I've modified darren at sullivan dot net script, so it can handle this behabiour, returning the
array correctly:
(I also added udddecode() to the QUERY_STRING, as the brackets appeared as %hex codes, makin'
parsing impossible..)
<?php
function f_multi_post_item($repeatedString, $as_mode='') {
// Gets the specified array of multiple selects and/or
// checkboxes from the Query String
//
// If $as_mode=='cbx' it will arrange the arrays assumming that there are
// missing cbx_param, the ones who were not checked in the form
//
$ArrayOfItems = array();
$arr_ret = array();
$ls_parms=urldecode($_SERVER["QUERY_STRING"]);
$raw_input_items = split("&", $ls_parms);
$li_total=0;
foreach ($raw_input_items as $input_item) {
$itemPair = split("=", $input_item);
if ($itemPair[0] == $repeatedString) {
$ArrayOfItems[] = $itemPair[1];
$li_total++;
}
}
if ($as_mode=='cbx') {
for($i=0; $i<$ArrayOfItems[$li_total - 1]; $i++) $arr_ret[$i]='0';
for($i=0; $i < $li_total; $i++) $arr_ret[$ArrayOfItems[$i]]='1';
} else {
$arr_ret=$ArrayOfItems;
}
return $arr_ret;
}
?>
Now, using the example, array will be correctly arranged:
<?php
$array_checkboxes=f_multi_post_item('cbx_foo', $as_mode='cbx');
?>
The array will content:
<?php
$array_checkboxes[0]=1 // (true) // Was checked !
$array_checkboxes[1]=0, // (false) // Was not checked !
$array_checkboxes[2]=1, // (true) // Was checked !
?>
Hope will help.
Juan Prada [aka 'countach' on usenet]