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
  • 22,092
  • 39
  • 79
  • 102

3 Answers3

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
  • 31,517
  • 8
  • 49
  • 87
0

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,834
  • 6
  • 44
  • 66
  • I just realised this method would make the query have to run many times, is there a better method to deleting using checkbox? –  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

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,453
  • 1
  • 14
  • 27