Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Re: Unknown number of check boxes?
Old
  (#1)
Jared Farrish
Guest
 
Posts: n/a
Default Re: Unknown number of check boxes? - 06-02-2007, 08:55 PM

Stephen Neigaard wrote:
> I would like to have a unknown number of generated check boxes like this:
>
> <input type="checkbox" name="chk01" />
> <input type="checkbox" name="chk02" />
> <input type="checkbox" name="chk0X" />
>
> And the name will be generated "chk01 to chk99", but how do I make the
> receiving PHP script that "scans" for post variables that are sent, so
> that I can work on this information?


Inspect this code example to see a way to handle this problem using magic
form variables in contained POST arrays:

<code>
<h4>Test of Multiple Checkboxes</h4>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<?php

function getCheckboxes() {
for ($i = 100; $i > 0; $i--) {
$tr = $i % 5 === 0 ? Array('','') : Array('<tr>','</tr>');
$str .= "<label><input type=\"checkbox\" " .
"name=\"form[checks][]\" value=\"$i\" /> Input
#$i</label>\n";
}
return $str;
}
echo(getCheckBoxes());

?>
<p><input type="submit" /></p>
</form>
<hr />
<pre>
<?php

if (!empty($_POST)) {
print_r($_POST);
}
?>
</pre>
<h4>Consuming of form post</h4>
<p>An example of inverting a posted checkbox array to support $checked[45]
=== true behavior, making it easier to access and test the posted
content</p>
<pre>
<?php

// This will return an array that has inverted the posted
// items that were in checkboxes and had a state of
// checked=true
function consumeFormChecks($arr) {
$consume = Array();
for ($i = 0; $i < count($arr); $i++) {
$consume[$arr[$i]] = true;
}
return $consume;
}
if (!empty($_POST)) {
print_r(consumeFormChecks($_POST['form']['checks']));
}else {
echo('<h4>Please select some random checkboxes above' .
' and submit the form</h4>');
}

?>
</pre>
</code>
--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Unknown number of check boxes?
Old
  (#2)
Jared Farrish
Guest
 
Posts: n/a
Default Re: Unknown number of check boxes? - 06-02-2007, 08:55 PM

On 5/28/07, Jared Farrish <EMAIL REMOVED> wrote:

> $tr = $i % 5 === 0 ? Array('','') : Array('<tr>','</tr>');



Ignore this line, it was from an earlier iteration of that function.

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top