0

I have an image upload form that looks like this: Gallery Screenshot

The code to generate tag checkboxes:

foreach($tag as $tagid => $tagname) {
  echo "<label><input type=\"checkbox\" name=\"img_tags[]\" value=\"".$tagid."\">".$tagname."</label>";
}

Then in PHP I want to read values of this checkboxes for each image. I want img_tags to be 2D array so I can loop over it and check values for each picture. Is this possible?

Output of:

$img_tags = $_POST['img_tags'];↵
foreach($img_tags as $t) {↵
    print_r($t);↵
}↵

is: 12121, which I don't understand at all.

2
  • You can use names like for image img_tags[pic1][name] and for tags img_tags[pic1][tags] Commented Sep 12, 2013 at 7:16
  • If you Google your exact title, you'll see your question is a duplicate of getting a checkbox array value from POST Commented Sep 12, 2013 at 7:16

1 Answer 1

0

If you want a 2d array in a $_POST variable then you need to express that in the names.

name="img_tags[level_1_key][]"
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I tried name=\"img_tags[".$counter."][]\" , where $counter is from while loop going through all images, and in PHP to get it : $img_tags = $_POST['img_tags['.$key.']'];. Generated HTML looks fine: name="img_tags[1][]" but I get Undefined index: img_tags[1].
That's because it is an array (which is what you asked for). $_POST['img_tags'][1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.