How to store state of a checkbox

3

I'm developing an application to make a school call list, I created Listview and populated it with data from a Mysql database, using Json . The problem is the following, I need to store the state of the selected checkbox . For example ':

The user has marked the checkbox fault% for Student 1 and checkbox of presence for student 2, how to store the status of check-box and then insert them into a frequency table in MySQL via JSON and PHP.

    
asked by anonymous 06.04.2014 / 05:28

2 answers

2

Two checkbox one for fault and one for presence, in this case it seems better a radio button since only one option can be checked. the m1, m2 ... values understand how the student's enrollment or code is the one of the bank. The must be done is to make that radius an array, adding brackets in name and leave either the index or the enrollment.

<?php
$alunos = array('m1' => 'joão', 'm2' => 'maria', 'm3' => 'juca', 'm4' => 'paula', 'm5' => 'fulano');
?>

<form action="gravar_falta.php" method="post">
    <?php foreach ($alunos as $matricula => $nome){ ?>
    <?php echo $nome; ?>
    presente
    <input type="radio" name="frequencia[<?php echo $matricula; ?>]" value="presente">
    falta
    <input type="radio" name="frequencia[<?php echo $matricula; ?>]" value="falta"><br>

    <?php } ?>  
    <input type="submit">
</form>

These radios will be sent to php with an array in this structure:

Array
(
    [frequencia] => Array
        (
            [m1] => presente
            [m2] => falta
            [m3] => falta
            [m4] => presente
            [m5] => falta
        )

)

Finally, just make a foreach to insert in the database:

$aula = 'português';
$frequencias = $_POST['frequencia'];

foreach ($frequencias as $aluno => $frequencia){
    insertFequencia($aula, $aluno, $frequencia);    
}
    
06.04.2014 / 06:14
1

One solution that I usually do to deal with this is to have a hidden field with the user code and even name, and in the checkboxes I concatenate their name with the hidden field code, so on the server side I know how many codes I can wait and check if the value of the checkbox came (true) or not (false)

I'll put an example of what it would look like in ASP:

HTML:

<tr>
     <td>
          <input type="hidden" name="hddCodigo" value="1"/>
          <input type="hidden" name="chkFalta1" value="1"/>
          <input type="hidden" name="chkPresenca1" value="1"/>
     </td>
</tr>
<tr>
     <td>
          <input type="hidden" name="hddCodigo" value="2"/>
          <input type="hidden" name="chkFalta2" value="1"/>
          <input type="hidden" name="chkPresenca2" value="1"/>
     </td>
</tr>

Server-Side / ASP

For Each iCodAluno In Request.Form("hddCodigo")
    'Se o check estiver marcado, o tamanho (Len) do valor será maior que 0 e a variável receberá True.
    bFalta = Len(Request("chkFalta" & iCodAluno)) > 0 
    bPresenca = Len(Request("chkPresenca" & iCodAluno)) > 0

    'Logica para atualizar os dados por aluno....
Next 
    
06.04.2014 / 06:00