Forum Moderators: coopster
One portion of this form is the check all the tags that the submission falls in.
For example, if the user is making a post on Bob Marley, possible tags they could check would be "music", "people", "culture", "70's".
<li><input type="checkbox" name="music" id="tag-music" /><label for="tag-music">Music</label></li>
<li><input type="checkbox" name="people" id="tag-people" /><label for="tag-people">People</label></li>
<li><input type="checkbox" name="culture" id="tag-culture" /><label for="tag-culture">Culture</label></li>
<li><input type="checkbox" name="70" id="tag-70" /><label for="tag-70">70's</label></li>
What I need to do is to figure out how to look at all the checkboxes that have been checked from a large list, and to one by one insert them into my MySQL table using the post's hash and corresponding tag. I need some kind of loop, that I don't need to recode if I add more tags in the future.
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-music"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-people"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-culture"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-to"]."'");
I'm at a loss as to where to start, or even if what I have is even good.
Any advice is most appreciated. Thanks.
I'm confused.
OK, I did more research on checkboxes. I know I want people to be able to select multiple, so I know checkboxes are the right form item.
Looking at this example [w3schools.com...] it looks like I would give all the checkboxes the same name, but different values.
<input type="checkbox" name="vehicle" value="Bike">
<input type="checkbox" name="vehicle" value="Car">
<input type="checkbox" name="vehicle" value="Airplane">
So I would have among all my other options:
<input type="checkbox" name="tag" value="music">
<input type="checkbox" name="tag" value="people">
<input type="checkbox" name="tag" value="culture">
<input type="checkbox" name="tag" value="70s">
Is that what you mean give them the name tag[]?
On the next page I'm having it it output all the contents submitted, using print_r($_POST). It looks like, even if I have multiple boxes selected, that only the last box in the list gets its value passed:
Array
(
[tag] => 70s
[x] => 44
[y] => 22
)
Is the html part of this script even right?
<input type="checkbox" name="tag[]" value="music">
.
.
then you'll get arrays with checked items as elements of those arrays.
in your first post the problem is you are using the id instead of the name
for your example above you need to add [] to each name like so
<input type="checkbox" name="tag[]" value="music">
<input type="checkbox" name="tag[]" value="people">
<input type="checkbox" name="tag[]" value="culture">
<input type="checkbox" name="tag[]" value="70s">
try that and it should work