Retrieve all checked checkboxes

2

I have this code that returns a list of the bank with a checkbox next to each, however when I send it via post it only takes the last one marked and not all:

Example with vardump where I marked all:

array(2) { ["seriais"]=> string(10) "3040156800" ["enviar"]=> string(9) "Solicitar" }

I just want to retrieve all checked checkboxes, and only returns the last.

FORM:

<form method="post" action="teste.php" id="ajax_form">
<table align="left">
<tr>        
<?

  $query_tpex = mysqli_query($con,"SELECT a.* , b.* , c.*,d.* , e.* FROM controle_contratos a, reparaveis b, projetos c, contratos d, empresa e WHERE a.status = 'Recebida' and a.id_contrato = '$numero_contrato' and a.id_pn = '$id_pn' and a.data_coleta <> '0000-00-00' and a.id_pn = b.id_pn and a.id_projeto = c.id_projeto and a.id_contrato = d.id_contrato and d.id_empresa = e.id_empresa");

  while($linha = mysqli_fetch_array($query_tpex)){

  ?>
    <br>
 <input type="checkbox" class='marcar' name="seriais"  value="<?=$linha['ordem_servico']; ?>" />

  <? echo "O.S:"." ".$linha['ordem_servico']."-------- Serial: ".$linha['sn']; ?></label>

  <? } ?>
  </tr>

  <tr><br>
  </button><button id='todos' type="button" onclick='marcardesmarcar();'>Marcar/Desmarcar</button>
    <br>    <label><input type="submit" class="but but-success" name="enviar" value="Solicitar" /></label>

    </form>
    
asked by anonymous 25.04.2017 / 16:42

1 answer

2

Place brackets in name of input :

<input type="checkbox" class='marcar' name="seriais[]"  value="<?=$linha['ordem_servico']; ?>" />

In this way it will send an array with all the selected values, which you can get in PHP with:

$seriais = $_POST['seriais'];
    
25.04.2017 / 16:59