I have a form with multiple checkboxes, which I want to put into an array. I go by the example provided here: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
So I've prepared the files:
checkboxes.php:
<form action="checkboxes2.php" method="post">
<input type="checkbox" name="tags[]" value="1" />1<br>
<input type="checkbox" name="tags[]" value="2" />2<br>
<input type="checkbox" name="tags[]" value="3" />3<br>
<input type="checkbox" name="tags[]" value="4" />4<br>
<input type="submit" value="Send" />
</form>
checkboxes2.php:
<?php
print_r($_POST["tags"]);
?>
Pretty simple...I realize I should only get the value of these textboxes and not if they have been selected or not. But I still get this error:
Undefined index: tags in checkboxes2.php on line 2
I have absolutely no idea what I did wrong here. I went by the example in the link above and did everything exactly the same (mainly copy/pasting and changing some parts like adding a submit button) but I don't get the output as shown in the example. I should at least get the values of each of these checkboxes, right?
What I want to do: I want to check the array of checkboxes, see which ones have been selected and add "yes" or "no" into a second array, like this:
<?php
$number1 = $_POST["tags"];
$number2 = array();
foreach($number1 as $number1_output)
{
if(isset($number1_output))
{
$number2[] = "yes";
}
else
{
$number2[] = "no";
}
}
print_r($number2);
?>
Well...it only half works. Only the checkboxes that have been selected are added to the array. So if I select "3" and "4" I get this:
Array ( [0] => yes [1] => yes )
What is the best way to deal with checkboxes in arrays and validating them?