How to process a variable amount of checkboxes with php
My goal is to process an unknown amount of names (between 2 and 8) and format it in a nice way. The amount of names varies per user. So one user will see 4 names, another will see only 2 names. I'll show an example with 3 names.
<input type="checkbox" name="inuser[]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[]" id="u3" value="Lotte" /><label for="u3">Lotte</label>
Say Rosa's checkbox is checked. Than I want to store 'Rosa' in the table. Say Rosa and Floris are checked. Now I want to store 'Floris and Rosa' Say all three are checked. Than I want to store 'Floris, Rosa and Lotte'
I'm not that experienced with arrays. So this is an unsolvable problem for me. I've tried to solve it by reading tutorials, but I need someone to help me a bit.
Let me try to explain why the amount of names are variable... On the site students will be able to log there school project activities. They work in groups, but the amount of people differ. So every 'project' will have a different amount of students working on it.
When they log an activity the students who ca开发者_如何转开发rried out the activity can be checked and the server should turn this into a string (for example: 'Floris, Rosa and Lotte') and store it in the log table. So later on the server will be able to retrieve the log information (What, When, Who, etc.)
Feel free to correct my terrible English. I'm from Holland.
Here's another code example that might help:
<?php
if(isset($_POST['inuser'])){
foreach($_POST['inuser'] as $name){
echo $name . "<br />";
}
}else{
$array = array("Floris", "Rosa", "Lotte");
$i = 1;
echo '<form action="test.php" method="post" name="names">';
foreach($array as $name){
echo '<input type="checkbox" name="inuser[]" id="u' . $i . '" value="' . $name . '" /><label for="u' . $i . '">' . $name . '</label>';
$i++;
}
echo '<input type="submit" name="Submit" value="Submit" /></form>';
}
?>
It´s not completely clear where you are having a problem exactly, but one of the problems you are facing (I think...), is that un-checked checkboxes are not posted when you post your form, so your array $_POST['inuser']
will have variable lengths and you cannot rely on the indices to access your values:
- if only
Lotte
is checked,$_POST['inuser'][0]
will be'Lotte'
- if
Floris
andLotte
are checked,$_POST['inuser'][0]
will be'Floris'
and$_POST['inuser'][1]
will be'Lotte'
- etc.
I would add an index to the form-fields so that you can easily check if a certain user is checked. Then the value does not really matter anymore and processing your array will be easier:
<input type="checkbox" name="inuser[ID_OF_FLORIS]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[ID_OF_ROSA]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[ID_OF_LOTTE]" id="u3" value="Lotte" /><label for="u3">Lotte</label>
Now you can check:
if (isset($_POST[$id_of_someone]))
{
// add this someone to the database
}
You can wrap a loop around it to loop through all your users.
Well, if you're looping through the possible checkboxs, then you can loop through the possible post data.
For instance:
<?
//this would be generated dynamically of course
$values = array(array("name" => "Floris",
"id" => "u1"),
array("name" => "Rosa",
"id" => "u2"),
array("name" => "Lotte",
"id" => "u3"));
foreach ($values as $value) {
echo '<input type="checkbox" name="inuser[' . $value['id'] . ']" id="' . $value['id'] . '" value="' . $value['name'] . '" /><label for="' . $value['id'] . '">' . $value['name'] . "</label>\n";
}
Will give you your check boxes (see here).
<input type="checkbox" name="inuser[u1]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[u2]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[u3]" id="u3" value="Lotte" /><label for="u3">Lotte</label>
Then for your post data:
<?
foreach ($values as $value) {
if (isset($_POST['inuser[' . $value['id'] . ']'])) {
// do things
}
}
Hope this is of use.
精彩评论