forms – How can I select multiple rows from a table and let the submission handler know which rows have been selected?

In a custom module, I have a form showing a table with multiple rows, as in the screenshot.


Once users select one or more rows, and they click on the Delete button, rows in the database should be deleted.

I’ve built a tableselect table with type. I cannot understand how to get the selected table rows in the submission handler.

How can I achieve this?

The following is the code I am using to build the form.

foreach ($result as $row) {
  $data() = (
      'data' => (
        '#theme'      => 'image',
        '#alt'        => 'catImg',
        '#uri'        => File::load($row->Image)->getFileUri(),
        '#width'      => 100,
    t("<a href="$row->id" class='db-table-button 
    db-table-button-edit use-ajax' data-dialog-type='modal'>Edit</a>"),
    t("<a href='delete-cat/$row->id' class='db-table-button
    db-table-button-edit use-ajax' data-dialog-type='modal'>Delete</a>"),

$header = (
  t('image'), t('Name'), t('Email'), t('Created'),
  t('Edit'), t('Delete'),

$build('table') = (
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $data,

$build('submit') = (
  '#type' => 'submit',
  '#name' => 'submit',
  '#value' => $this->t('Delete'),