How to list checbox data?

Asked

Viewed 70 times

0

My code is 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 got 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>
  • 1

    Welcome to stackoveflow in English, explain better what you want. What’s the problem you’re having? A brief summary of what is happening would be nice.

  • i need the user to be able to select several areas and a technician to register. e ne enrolei in the array

1 answer

1

How you want to save the "areas" in a single field of the database but they come in a array, have to gather all the entries from the array in a string and that’s where the function 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

Despite being more extensive and performing the same as the function implode().

  • I loved the fix! it really worked out. and in case I want to see what’s in the database? I have a code but it’s not rolling.

Browser other questions tagged

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