1

I have been searching on this topic and the more I read the more confused I get. I hope you can help me on this.

My objective is to add checkboxes to a table in order to be able to delete the rows I select from that table. So my code til now is like this:

if ($arch = $pdo->prepare("SELECT name, age FROM table WHERE id = ?)) { 
  $arch ->execute(array($id));
  $data = $arch->fetchAll();  
  echo '<div class="coolTable" ><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>';
    foreach ($data as $row){
    echo '<tr>';
      foreach ($row as $col){
      $col=nl2br($col);
      echo '<td>'.$col.'</td>';
      }
    echo '<td><input type="checkbox" name="checkbox" value="" id="checkbox"></td>';
    echo '</tr>';
    }
  echo '</table></div>';
}

With this, I have all my checkboxes in place. But now, how can I submit the checkboxes with a Post so I can Delete the rows I checked?

I suppose I can use an array to name each checkbox? but don`t know how :-s

Thanks in advance for your help!

1

1 Answer 1

3

Change this line:

echo '<td><input type="checkbox" name="checkbox" value="" id="checkbox"></td>';

with

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

You can access every value of checked checkbox using this array $_POST['checkbox'] if you are using POST method or $_GET['checkbox'] for GET method.

PS: You should give your checkbox's a value.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, but tell me please how do I associate the Id of the row that has been checked, so I can delete it later?
Put the id as value of your checkbox !

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.