How to list checbox data?

0

My code looks like this:

<!-- caixa de seleção de técnicos -->
</style> 
<body> 
<p>T&eacute;cnico</p>
<form method="post" action="salvar.php">
<b><select name="nome">
<option value="vazio"></option> 
<option value="Andre">Andre Silveira Da Silva - N5692999</option>
<option value="Deivis">Deivis Ingracio Maciel - N5744364</option>
<option value="Josoe">Josoe Alves Rodrigues - N5688572</option>
<option value="Nardi">Leandro Nunes Nardi - N5631153</option>
<option value="Lucas">Lucas Vieira Noble - N4011863</option>
<option value="Cabreira">Marcel Rodrigues Cabreira - N5722079</option>
<option value="Matheus">Matheus Rosa Da Silva - N5781269</option>
<option value="Paulo">Paulo Guilherme Da Luz Santos - N5766893</option>
<option value="Willian">Willian Moraes Dos Santos - N5720370</option>
</select>


<p><label>&Aacute;reas: <br>
<!-- caixas de "radio button" das áreas -->

 <form method="post"  action="salvar.php">
<input name="marcar[]" type="checkbox"id="marcar[]" value="Grande_Poa">Conurbadas<br><br>

<tr><input name="marcar[]" type="checkbox" id="marcar[]" value="area1">&Aacute;rea 1</tr>

<input name="marcar[]" type="checkbox"id="marcar[]" value="area2">&Aacute;rea 2<br>

<tr><input name="marcar[]" type="checkbox"id="marcar[]" value="area3">&Aacute;rea 3</tr>

<input name="marcar[]" type="checkbox"id="marcar[]" value="area4">&Aacute;rea 4<br>

<tr><input name="marcar[]" type="checkbox"id="marcar[]" value="area5">&Aacute;rea 5</tr>

<input name="marcar[]" type="checkbox"id="marcar[]" value="area6">&Aacute;rea 6<br>

<tr><input name="marcar[]" type="checkbox"id="marcar[]" value="area7">&Aacute;rea 7</tr>

<input name="marcar[]" type="checkbox"id="marcar[]" value="area8">&Aacute;rea 8<br>

<tr><input name="marcar[]" type="checkbox"id="marcar[]" value="area9">&Aacute;rea 9</tr>

<input name="marcar[]" type="checkbox"id="marcar[]" value="area10">&Aacute;rea 10<br><br>

<tr><input name="marcar[]" type="checkbox"id="marcar[]" value="LOGs">LOG's</tr>
</form>
</label></p> 
<!-- Botões -->
<div align="center">
<input type="submit"name="salvar" value="Salvar" />

<input type="button"name="listar" value="Visualizar" /> 

<input type="button"name="excluir" value="Excluir" />

</form>

Data to save looks like this:

<?php
@ini_set('display_errors', '1');
error_reporting(E_ALL);

$nome  = $_POST["nome"]; 
$areas = $_POST["marcar"];
foreach($_POST['marcar'] as $marcar) {   
    echo "Valor Recebido: ".$marcar;  
}

mysql_connect("localhost", "root", "");
mysql_select_db("tcc");

mysql_query("INSERT INTO tratamentos ( id_tratamentos, nome , areas )  
            VALUES ( NULL , '$nome','$areas')");
mysql_close();
echo "Salvo com sucesso<tr><br />";
?>
<a href="index.php">Voltar</a>
    
asked by anonymous 13.11.2014 / 04:43

1 answer

1

Since you want to save the "areas" in a single database field but they come in array , you have to join all the array entries in a string and that's where implode() of PHP enters:

$areas = implode(",", $_POST["marcar"]);

Your current code could also look like this:

$areas = '';
foreach ($_POST['marcar'] as $marcar) {   
    $areas.= $marcar.',';  
}
$areas = rtrim(",", $areas); // retirar a virgula do fim da string

Although it is more extensive and performs the same as the implode() function.

    
13.11.2014 / 10:53