Get checkbox values in an array

3

How do I get the value of the checkbox in the code below?

It is a presence list and this code I modified for one that I use a little different and this way I do not know how to get the values of checked checkboxes in order to be able to in each name in the table to cast the date in each name.

I type the names of the table and those that are present I mark the checkbox and, when saving, the date will be written in every name that is marked.

Follow the code:

<table width="500" border="0"  bordercolor="#B9B3B3" cellspacing=0 cellpadding=2 align="center">
    <tbody>
    <tr align="center">
              <?php
				
		$getfamilia = "SELECT * FROM $tabela";
		$getfamiliaquery = $mysqli->query($getfamilia);
		while($getfamilialine = mysqli_fetch_array($getfamiliaquery)) 
		{ 
			$fn1="Em Andamento";
			$familia = $getfamilialine['nome'];
			$familia_id = $getfamilialine['id'];
			$fn=$getfamilialine['finalizado'];
			if(strcasecmp($fn, $fn1) == 0)
			{
			echo "
			<tr>
					<td width='250' align='center'>$familia </td>
					<td width='250'  align='center'> <input type='checkbox' name='op'></td>
					
					</tr>
			";
			}
		}
		
		echo "</table>"; /*fecha a tabela apos termino de impressão das linhas*/
print_r($mysqli->error);
if($mysqli->connect_errno)
{
	echo"Falha na conexao";
}

else
{
	
}
	?>
    <br>
     <input type="submit" value="SALVAR" />
    </tr>
</tbody>
</table>
    
asked by anonymous 18.10.2015 / 21:02

4 answers

1

You should put Checkbox's name with [] Example:

<input type='checkbox' name='op[]'>

and to get the data, already created the array, you just have to

implode(',', $_POST['op']);

If you want to know more, see: link

    
18.10.2015 / 22:23
1

Kelly, I just did not pass the values to the variables $familia and $familia_id , I did it right, but feel free.

The concept is exactly the one we discussed, the magic is in value='".$getfamiliaquery["id"]."' name='op[]' value stores the family id in the while loop, and name stores with each loop setting indexes that will be retrieved via POST like this: $_POST['op'][0] , $_POST['op'][1] , $_POST['op'][2] , and so on.

<form action="" method="POST">    
    <table width="500" border="0"  bordercolor="#B9B3B3" cellspacing=0 cellpadding=2 align="center">
        <tbody>
            <tr align="center">
            <?php

            $getfamilia = "SELECT * FROM $tabela";
            $getfamiliaquery = $mysqli->query($getfamilia);

            while($getfamilialine = mysqli_fetch_array($getfamiliaquery)) {
                if($getfamilialine["finalizado"] == "Em Andamento") {
                    echo "
                        <tr>
                            <td width='250' align='center'>".$getfamilialine["nome"]."</td>
                            <td width='250' align='center'><input type='checkbox' value='".$getfamilialine["id"]."' name='op[]'></td>
                        </tr>";
                }
            }

            echo "</table>";
            ?>
            <br />
            <input type="submit" name="salvar_ok" value="SALVAR" />
            </tr>
        </tbody>
    </table>
    <br />
    <?php
        // Aqui imprime o resultado retornado via POST
        if($_POST["salvar_ok"]) { // Se clicar no botão SALVAR executa o bloco abaixo
            for($i = 0; $i < count($_POST["op"]); $i++) { // Loop enquanto houver conteúdo na array
                echo $_POST["op"][$i]."<br />"; // $_POST["op"][0], $_POST["op"][1], $_POST["op"][2], $_POST["op"][3]........ 
            }
        }
    ?>
</form>
    
19.10.2015 / 00:36
1

First of all, your code needs some adjustments, I'll reformat it below. For you to get a collection through a post method, you need to treat your inputs as an array and capture them in the same way, to do this, formatted your code as follows:

<?php

if ($mysqli->connect_errno) {
    echo "<pre>Falha na conexão:\n";
       print_r($mysqli->error);
    echo '</pre>'; die();
}

if ($_POST) {
  if (count($_POST['op'])) {
       foreach ($_POST['op'] as $key => $selecionado) {
          $familia_id = $_POST['familia_id'][$key];
          $familia    = $_POST['familia'][$key];
          $status     = $_POST['status'][$key]
          $addFamily  = "INSERT INTO $tabela (id, nome, finalizado)
                         VALUES ('$familia_id','$familia','$status')";
          $addFamilyQueryByStatus = $mysqli->query($addFamily);
      }
  }
  echo "<p>Salvamento processado com sucesso!</p>";
 die();
}

$getfamilia = "SELECT * FROM $tabela where finalizado = 'Em Andamento'";
$getfamiliaquery = $mysqli->query($getfamilia);

 ?> 
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="salvar">
<table width="500" border="0" bordercolor="#B9B3B3" cellspacing="0" cellpadding="2" align="center">
    <tbody>
        <?php
        if ($getfamiliaquery) {
            while ($getfamilialine = mysqli_fetch_array($getfamiliaquery)) { 
               $fn1        = "Em Andamento";
               $familia    = $getfamilialine['nome'];
               $familia_id = $getfamilialine['id'];
               $fn         = $getfamilialine['finalizado'];

                echo "
                 <tr>
                    <td width=\"250\" align=\"center\">{$familia}</td>
                    <td width=\"250\" align=\"center\">
                      <input type=\"checkbox\" name=\"op[]\" value="1">
                      <input type=\"hidden\" value="{$fn}" name=\"status[]\">
                      <input type=\"hidden\" value="{$familia}" name=\"familia[]\">
                      <input type=\"hidden\" value="{$familia_id}" name=\"familia_id[]\">
                    </td>
                </tr>
                ";
            } //fim do loop
        } else {
          echo "<p>Não há dados em andamento para serem processados.</p>";
        }
       //ob: você já fechou a tabela logo abaixo.
    ?>
     <tr>
        <td colspan="2" style="float:right">
          <input type="submit" value="SALVAR" />
       </td>
    </tr>
</tbody>
</table>
</form>
  

    
19.10.2015 / 13:46
1

the code that is on the other page

<?php
$nome=$_POST['familia'];
$data=$_POST['date'];
$id=$_POST['familia_id'];
$tabela=$_SESSION['tabela'];
$qtd=$_SESSION['qtd'];


if($_POST["salvar_ok"]) {
            for($i = 0; $i < count($_POST["op"]); $i++) 
			
			{
               echo"$data";
			   echo"$qtd";
			   
			   
			   
			   
			   if($qtd=="NULL")
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d1) VALUES('$data')");
			   
			   echo"$data";
	
		       print_r($mysqli->error);
			   
			   }
			   
			   if($qtd=='1')
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d2) VALUES('$data')");
	
		       print_r($mysqli->error);
			   
			   }
			   if($qtd=='2')
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d3) VALUES('$data')");
	
		       print_r($mysqli->error);
			   
			   }
			   if($qtd=='3')
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d4) VALUES('$data')");
	
		       print_r($mysqli->error);
			   
			   }
			   if($qtd=='4')
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d5) VALUES('$data')");
	
		       print_r($mysqli->error);
			   
			   }
			   if($qtd=='5')
			   
			   {
				   
			   
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d6) VALUES('$data')");
	
		       print_r($mysqli->error);
			   
			   }
			   if($qtd=='6')
			   
			   {
				   
			   $fn="sim";
			   $sql= $mysqli->query(" INSERT IGNORE INTO $tabela (d7, finalizado) VALUES('$data', '$fn')");
			 
		       print_r($mysqli->error);
			   
			   }
			   
			   
            }
        }

if($mysqli->connect_errno)
				{
				echo"Falha na conexao";
				}

			else
				{
	
				
				}



print '<pre>';
var_dump($_SESSION);
print '</pre>';

?>
    
19.10.2015 / 14:30