4

I have a HTML schema like this:

enter image description here

Link: https://i.sstatic.net/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

7 Answers 7

3

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";
Sign up to request clarification or add additional context in comments.

5 Comments

I have the property value assigned to the "position" that it should be, but I don't find the proper PHP algorithem to do it properly, PS: now I get your point, I'm going to try it.
@Daniel: Why this won't work? Now he has always a value to all checkboxes and can iterate through them without risking index offset error EDIT: Ok now i understand. Changed the name of the checkboxes.
so if I do arrayLunes[] it wont work? I must write the index? I say it because I'm adding rows and deleting rows by javascript and I need to do it more simple I can do it.
No you can't if you want to receive a value for every existing checkbox. If you add or remove a row, simply iterate through all existing rows and reset the index.
Okay, it is working as I desired, I put un the checkbox value, the position where it is, and then in the hidden i put -1 (because I use the 0 as the first position), so if I find -1 is unchecked, thanks for that tip.
3

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";
}

Comments

1

If you have a unique ID in your DB for each row, add it in the parenthesis for each column, combined with the hidden element trick to always get the POST:

<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" />

2 Comments

It's in order to insert in the database, all is handled by javascript, adding/deleting rows, and then I have to check everything if checked or not in php.
If rows are added by javascript, it is just as easy to manage the ID there instead of in the DB. Start a counter variable at 1 (or biggest ID displayed if you loaded data) and add 1 for every new row added.
1

Try this,

foreach($_POST['arrayLunes']) as $index=>$value)
    echo ($value == 'on' ? "YES" : "NO").' index='.$index;

Read getting a checkbox array value from POST

2 Comments

I don't use a foreach because I wanted to know the index in purpouse, just for testing, and the main problem is that if a checkbox is not checked, the post array wont store it, so I need to have stored but with false value.
Test the above I've made changes in it.
1

I have no idea how your HTML is built, but let's say you have these checkboxes:

<input type="checkbox" name="check[]" value="a" />
<input type="checkbox" name="check[]" value="b" />
<input type="checkbox" name="check[]" value="c" />
<input type="checkbox" name="check[]" value="d" />
<input type="checkbox" name="check[]" value="e" />

In this case, if all of them are posted, $_POST['check'] will contain 0=>a, 1=>b and so on.

If only first and last are checked you will have 1=>a, 2=>e, and you need to see which are not checked (b,c,d)

My solution is the following:

Get from the HTML all the checkboxes and compare to the posted ones:

$doc = new DOMDocument();
$doc->loadHTMLFile('test6.php');
$cboxes = $doc->getElementsByTagName('input');
foreach ($cboxes as $cbox) {
       if($cbox->getAttribute('type') == 'checkbox') {
           $cb[] = $cbox->getAttribute('value');
       }
}
$differences = array_diff($cb, $_POST['check']);
var_dump($differences);

If a and e are posted, this will output:

array (size=3)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)

I forgot to mention it can track the difference in array keys too, not only the values, for example if b and c are posted (1 and 2 keys), the output will be:

array (size=3)
  0 => string 'a' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)

so unposted keys are 0,3,4

$differences = array_diff($cb, $_POST['check']);
var_dump($differences); // unposted checkboxes with relevant keys
$diff1 = array_diff($cb,$differences);
var_dump($diff1); // posted checkboxes with relevant keys

5 Comments

It doesn't matter how checkboxes are generated, you will only need one more check if($cbox->getAttribute('name') == 'arrayLunes[]') { to compare to $_POST['arrayLunes'] (array_diff) and arrayMartes[] to its $_POST['arrayMartes'];
see my last edit, differencing one more time the output can produce you the posted checkboxes with they relevant keys in the order
In any case I edited my post so probably you'll get more information about what I'm doing, I'm reading all answers and testing yet
tell me if you find any difficulcies
I think that solution is very difficult, see the TobiasKun answer, it's very simple and working, thanks for your help anyway =)
0

The easiest workaround this is to use an hidden input as value=0 let's say that has the same name as the checkbox following it.

If the checkbox is checked, you will see its value, if not, the input's value will then be used.

Comments

0

IF you dont want to use JS - you could do this

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

This way you always receive a $_POST for a checkbox value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.