How to store checkbox status

Asked

Viewed 1,288 times

3

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

The user has marked the checkbox Student 1 and the checkbox 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.

  • Post the code of form or of checkbox.

  • All screen elements are objects accessible by Javascript, so the state is always there as long as the screen does not reload

2 answers

2


Two checkbox one for foul and one for presence, in this case it seems better one radio button because only one option can be checked. values m1, m2 ... Understand as the enrollment or code of the student who from the bank. What should be done is make this radio an array, adding brackets in the name and let either the index be the matricula.

<?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 do a foreach to insert into the bank :

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

foreach ($frequencias as $aluno => $frequencia){
    insertFequencia($aula, $aluno, $frequencia);    
}
  • With regards to sending and storage you helped me a lot, however I’m having difficulties in coding in Java. How can I reference a Radio Button to a student(Bd Log) and then store it in an array ?

  • Checkbox chk = (Checkbox) v.findViewById(R.id.chkAlunos); chk.setTag(Students); chk.setOnClickListener(new View.Onclicklistener() { @Override public void onClick(View v) { Checkbox chk = (Checkbox) v; String pupil = (String) chk.getTag(); if (chk.isChecked()) ????

1

One solution I usually do to deal with this is to have an Hidden field with the user code and same name, and in the checkboxes I concatenate their name with the Hidden field code, this way on the server side I know how many codes I can expect and check if the checkbox value came (true) or not (false)

I’ll put an example of what it would be 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 
  • Rogerio , Thanks for your attention So, you have an example code for this solution ? I don’t know if I made it clear , but like I need to store the status of checkboxes and then insert them into the database to later check the students' absences and attendance.

  • @Danilo, I updated the answer with an ASP solution that would be quite possible to do in PHP or JSP, just adapt !

Browser other questions tagged

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