Reputation: 22405
I have a function that takes in 2 params ($group, $array)
$group = the 'name' of the input attribute $array = the array of checkbox values
function checkboxes($group, $array) {
$string = NULL;
$group = (string)$group."[]";
foreach($array as $key => $value) {
$string .= "<br /><input type='checkbox' id=".$value." name='".$group."' value='".$value."' /><label for=".$value.">$value</label>";
}
return $string;
}
What I'm trying to do is convert that into proper HTML, so that I can process the value in a script.
here is the input:
checkboxes("class", array("Warrior","Mage","Priest","Rogue"));
$boxes = $_POST['class'];
for ($i=0; $i<count($boxes); $i++) {
echo $boxes[$i];
}
Output: "R" (when more than one are checked as well)
Any help would be great, sorry if I forgot anything.
This is homework, so don't give me answers that break the stackoverflow terms/my schools honor code please!
Upvotes: 0
Views: 1770
Reputation: 6003
Try this. It seems to print all checked boxes.
<?php
function checkboxes($group, $array) {
$string = NULL;
$group = (string)$group."[]";
foreach($array as $key => $value) {
$string .= "<br /><input type='checkbox' id=".$value." name='".$group."' value='".$value."' /><label for=".$value.">$value</label>";
}
return $string;
}
if( isset( $_POST[ 'class' ] ) ) {
$boxes = $_POST['class'];
for ($i=0; $i<count($boxes); $i++) {
echo $boxes[$i] . '<br />';
}
}
?>
<form action="t23.php" method="POST">
<?php
echo checkboxes("class", array("Warrior","Mage","Priest","Rogue"));
?>
<input type="submit" name="btnOutput" value="submit"/>
</form>
Upvotes: 1
Reputation: 39724
I don't know how your arrangement is in you page but:
<?php
if(isset($_POST) && !empty($_POST['class'])){
echo 'SELECTED: <br /><br />';
$boxes = $_POST['class'];
for ($i=0; $i<count($boxes); $i++) {
echo $boxes[$i].'<br />';
}
}
function checkboxes($group, $array) {
$string = NULL;
$group = (string)$group."[]";
foreach($array as $key => $value) {
$string .= "<br /><input type='checkbox' id=".$value." name='".$group."' value='".$value."' /><label for=".$value.">$value</label>";
}
return $string;
}
echo '<form method="post">';
$boxes = checkboxes("class", array("Warrior","Mage","Priest","Rogue"));
echo $boxes;
echo '<input type="submit">';
?>
Upvotes: 0