2

I cannot seem to understand why this is not echoing:

foreach($_POST["checked"] as $value) {
    echo "$value";
}

When I just use the following:

echo $_POST['checked'];

The value is shown but only for 1 checkbox.

I need to grab all the values of all checked checkboxes.

This is my checkbox:

echo '<td><input id="checked" name="checkbox[]" type="checkbox" value="'.$row['id'].'"></td>';
ZygD
  • 10,844
  • 36
  • 65
  • 84
Elevant
  • 957
  • 1
  • 12
  • 32

3 Answers3

1

You are considering id attribute where as it is name attribute that gets posted.

Change it to:

foreach($_POST["checkbox"] as $value) {
  echo $value; // Also, no need of double quotes.
}

PHP variable interpolation vs concatenation

Community
  • 1
  • 1
Pupil
  • 23,528
  • 5
  • 42
  • 64
  • I just realised this method would make the query have to run many times, is there a better method to deleting using checkbox? – Elevant May 04 '15 at 06:34
  • Simply, get array of checkbox ids and implode them into a string eg. '12,78,87'. In Delete query, fire query like 'DELETE FROM [table_name] WHERE id IN (checkbox_ids_String)' – Pupil May 04 '15 at 06:36
0

The field are named as checkbox[] not checked.

echo '<td><input id="checked" name="checkbox[]" type="checkbox" value="'.$row['id'].'"></td>';

Try with -

foreach($_POST["checkbox"] as $value) {
    echo "$value";
}
Sougata Bose
  • 30,871
  • 8
  • 44
  • 87
0

after using foreach $value will also be an array like something like below

foreach($_POST['checkbox'] as $value) {
        echo "<pre>";
        print_r($value);
    }
Vivek Singh
  • 2,445
  • 1
  • 13
  • 26