Reputation: 303
I am trying to store the values from checkboxes inside my database. So far, I was only able to store the last checkbox choice. Afterwards, I tried following someone's example, but I got so confused. How could I ensure that all my data is being stored inside the database?
In my table, my column which I want to contain the information is named "categories". As for the rest, it is below (2 of them only)
<tr>
<td><input type="hidden" name="categories" value="categories">Categories</td>
<td>
<div class="checkbox";>
<label class="checkbox_label">
<input name="cats" value="Accessoires" type="checkbox" class="checkbox1"/>
Accessoires
</label>
</div>
<div class="checkbox";>
<label class="checkbox_label">
<input name="cats" value="Mobilier de bureau " type="checkbox" class="checkbox1"/>
Mobilier de bureau
</label>
</div>
</td>
</tr>
This is the code I tried following:
$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];
$str = $ledlamps . ": " . implode(", ", $box);
And so I ended up with this:
$cats = $_POST['cats'];
$categories=$_POST['categories'];
$str = $categories . ": " . implode(", ", $cats);
Upvotes: 0
Views: 172
Reputation: 5143
I do not have sufficient privileges to add a comment to the previous answer attempt but what @Erman Belegu was saying is right, cats[]
should do the trick. Here is the code.
The HTML
<tr>
<td><input type="hidden" name="categories" value="categories">Categories</td>
<td>
<div class="checkbox";>
<label class="checkbox_label">
<input name="cats[]" value="Accessoires" type="checkbox"
class="checkbox1"/>
Accessoires
</label>
</div>
<div class="checkbox";>
<label class="checkbox_label">
<input name="cats[]" value="Mobilier de bureau "
type="checkbox" class="checkbox1"/>
Mobilier de bureau
</label>
</div>
</td>
</tr>
<input type="submit" name="submit" value="submit"/>
The only thing I added was the submit button so I could only do something in the php when submit is clicked, here is the same php code.
<?php
$cats = array();
if(isset($_POST['submit']))
{
if(isset($_POST['cats']))
{
$cats = $_POST['cats'];
}
$categories= $_POST['categories'];
$str = $categories . ": " . implode(", ", $cats);
echo $str;
}
?>
If you select something and click echo you will see categories: and whatever you selected.
Hope this helps or perhaps I did not get exactly what your problem was in the first place.
Thanks.
Upvotes: 1
Reputation: 4079
The name of input shoud be name="cats[]"
. In these form you will get array of value that you checked.
Upvotes: 0