Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop a list of checkboxes that some probabbly are not checked in PHP

I have a HTML schema like this:

enter image description here

Link: http://i.stack.imgur.com/9RRpx.jpg

CheckBox names: Lunes column name = arrayLunes[] then, Martes column name = arrayMartes[], etc...

First, I want to test Lunes list (monday), if I have checked the first and the third, the array will have only [0] and [1] array positions, but I want for example: [0] = true, [1] = false, [2] = true, [3 .. x] = false

Something like this PHP Code, that obviously won't work, because if a checkbox is not checked will not send by POST, so it will be index offset error

for ($c = 0; $c < count($_POST['arrayLunes']); $c++)
    echo ($_POST['arrayLunes'][$c] == 'on' ? "YES" : "NO");

Conclusion: So, now, the $_POST['arrayLunes'] variable only will contain in order the checkbox checked and I need the not checked one too, in his respective position.

How can I do or how can I simulate it?

EDIT

My HTML code is something like this

<div style="margin-left: 5px; padding: 5px;">
    <input class="btnFranjas" type="button" value="- Quitar franja" onclick="removerFranjaCalendario();" />
    <input class="btnFranjas" type="button" value="+ Añadir franja" onclick="addFranjaCalendario();" />
    <input class="btnFranjas" type="button" value="Reestablecer" onclick="reestablecerFranja();" />
</div>
<form action="index.php?zona=plataforma&id=<?php echo $_GET['id']; ?>&acceso=<?php echo $_GET['acceso']; ?>" method="post">
    <table id="tablaCalendario">
        <thead>
            <th>Horario</th>
            <th>Lunes</th>
            <th>Martes</th>
            <th>Miércoles</th>
            <th>Jueves</th>
            <th>Viernes</th>
            <th>Sábado</th>
            <th>Domingo</th>
        </thead>
        <tbody>
            <tr style="background: #E0E6F8;">
                <td>
                    <table>
                        <tr>
                            <td><b>Inicio: </b></td>
                            <td>
                                Hora
                                <select id='arrayInicioHora[]' name='arrayInicioHora[]'>
<?php
                                    for ($c = 0; $c < 24; $c++)
                                        echo "    <option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                            <td>
                                Minuto
                                <select id='arrayInicioMinuto[]' name='arrayInicioMinuto[]'>
<?php
                                    for ($c = 0; $c < 60; $c++)
                                        echo "<option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><b>Fin: </b></td>
                            <td>
                                Hora
                                <select id='arrayFinHora[]' name='arrayFinHora[]'>
<?php
                                    for ($c = 0; $c < 24; $c++)
                                        echo "<option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                            <td>
                                Minuto
                                <select id='arrayFinMinuto[]' name='arrayFinMinuto[]'>
<?php
                                    for ($c = 0; $c < 60; $c++)
                                        echo "    <option value='" . $c . "'>".($c > 9 ? $c : "0" . $c)."</option>";
?>
                                </select>
                            </td>
                        </tr>
                    </table>
                </td>
                <td align="center"><input type="checkbox" name="arrayLunes[]" id="arrayLunes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorLunes[]" id="arrayValorLunes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayMartes[]" id="arrayMartes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorMartes[]" id="arrayValorMartes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayMiercoles[]" id="arrayMiercoles[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorMiercoles[]" id="arrayValorMiercoles[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayJueves[]" id="arrayJueves[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorJueves[]" id="arrayValorJueves[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayViernes[]" id="arrayViernes[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorViernes[]" id="arrayValorViernes[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arraySabado[]" id="arraySabado[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorSabado[]" id="arrayValorSabado[]" style="width: 60px;" /></td>
                <td align="center"><input type="checkbox" name="arrayDomingo[]" id="arrayDomingo[]" value="0" /> <input type="text" placeholder="Valor" name="arrayValorDomingo[]" id="arrayValorDomingo[]" style="width: 60px;" /></td>
            </tr>
        </tbody>
    </table>
    <input class="orangebutton" type="submit" name="enviarCalendario" id="enviarCalendario" onclick="return confirmacionAccion();" value="Enviar calendario" />
</form>

And my javascript for this sampe: http://pastebin.com/eKFwMFvD

like image 428
Alejandro L. Avatar asked Jan 12 '23 23:01

Alejandro L.


2 Answers

There is a trick so you always receive a value for a checkbox:

<input type="hidden" name="arrayLunes[1]" value="0">
<input type="checkbox" name="arrayLunes[1]" value="1">

<input type="hidden" name="arrayLunes[2]" value="0">
<input type="checkbox" name="arrayLunes[2]" value="1">

So if the checkbox is checked you receive 1 as value and if not you will get the 0.

EDIT:

Like Daniel said you have more than one arrayLunes. So you have to add an index manually to the array notation. See above.

To iterate through your checkboxes do this:

foreach($_POST['arrayLunes'] as $val)
    echo $val ? "YES" : "NO";
like image 87
Tobias Golbs Avatar answered Jan 29 '23 01:01

Tobias Golbs


The best solution to this, in my opinion, is to use isset() instead of a comparison with the value of the checkbox.

However, in order to do this, you will need to know through some other mechansim how many checkboxes are on the page.

So when you are generating the page, I suggest you add:

<input type="hidden" name="rowcount" value="<?=$x?>">

...somewhere within the form that contains the table rows, where $x is the number of rows created on the page.

That way, the code that receives the form can simply do this:

for ($c = 0, $count = (int) $_POST['rowcount']; $c < $count; $c++)
{
    echo isset($_POST['arrayLunes'][$c]) ? "YES" : "NO";
}
like image 20
DaveRandom Avatar answered Jan 29 '23 02:01

DaveRandom