A_funs
A_funs

Reputation: 1246

Recovering POST checkbox values in an array

Somewhat new to php, I did some searching but didn't find a clear answer. I have a page with automatically generated checkboxes coming from a read CSV file:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $row++;
        echo '<tr><td>' . $data[0] . '</td><td><input type="checkbox" name="included" value="col' . $row . '" /></td></tr>';
        echo "<br>";
        }

This form will submit to a page, and I want to get an array of the checked boxes, like "col2" "col4" "col5"

How do I do this?

Upvotes: 1

Views: 126

Answers (4)

Damien
Damien

Reputation: 5882

You need to change the name of your checkbox to included[].

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  $row++;
  echo '<tr><td>' . $data[0] . '</td><td><input type="checkbox" name="included[]" value="col' . $row . '" /></td></tr>';
  echo "<br>";
}

And then read the checked list with $_POST['included'] (which will be an array).

Upvotes: 2

Ilians
Ilians

Reputation: 743

Change the name from included to included[]

Upvotes: 0

jbrtrnd
jbrtrnd

Reputation: 3843

Name your inputs included[].

Upvotes: 1

Jon
Jon

Reputation: 437584

Change the name to name="included[]" and you will get an array when the form is submitted.

Upvotes: 1

Related Questions