Pass Array value by checkbox

Asked

Viewed 31 times

1

I have this checkbox:

<input type="checkbox" name="seleciona[]" value="<?= $row_questao['codprova'] ?>" >

I have a query that returns multiple records and each record has one checkbox for this line, I need that when marking some it send via POST the values to another page.

I’ve got it done so far:

if ($_POST) {
   foreach ($_POST["seleciona"] as $checkSeleciona) { // linha 16
       echo $checkSeleciona . ' - ';
   }
}  

But the page when giving the Submit it returns with the following error:

Notice: Undefined index: seleciona in C:\xampp\htdocs\sav\cadastraProva.php on line 16

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\sav\cadastraProva.php on line 16

1 answer

0


This is because you are sending the form without checking any checkbox, and with that the array $_POST["seleciona"] does not exist. When a checkbox is not checked, it is not sent in the POST.

The if ($_POST) is being validated because another element is sent in the form other than the checkbox (eg. , some other input, select, or any form element with the attribute name).

Change the if for if ($_POST["seleciona"]) to do the foreach only if the array exists.

  • Perfect @Sam, vlw same. the div of the checkbox was out of the form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.