Complete a list of checkboxes that some probably don't check in PHP

I have a HTML scheme 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.

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

Something like this PHP code, which obviously will not work, because if the checkbox is not selected, there will be no POST sending, therefore 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 variable $_POST['arrayLunes'] will only contain checkbox checked , and I also do not need to check it in its corresponding position.

How can I do this or how to imitate 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

+4
source share
7 answers

There is a trick, so you always get the value for the 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 get 1 as the value, and if not, you get 0 .

EDIT:

Like Daniel said you have more than one arrayLunes. Therefore, you need to add the index to the array notation. See above.

To iterate through your checkboxes, follow these steps:

 foreach($_POST['arrayLunes'] as $val) echo $val ? "YES" : "NO"; 
+3
source

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

However, for this you will need to find out, through several other values, how many flags are on the page.

So, when you create the page, I suggest adding:

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

... somewhere inside a form containing table rows, where $x is the number of rows created on the page.

So the code that gets the form can simply do this:

 for ($c = 0, $count = (int) $_POST['rowcount']; $c < $count; $c++) { echo isset($_POST['arrayLunes'][$c]) ? "YES" : "NO"; } 
+3
source

Try it,

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

Read receipt of checkbox cell value from POST

+1
source

If you have a unique identifier in your database for each row, add it to the parenthesis for each column, combined with the trick of the hidden element, to always get 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" /> 
+1
source

I don't know how your HTML is built, but let 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 sent, $ _POST ['check'] will contain 0 => a, 1 => b, etc.

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

My solution is as follows:

Get all checkboxes from HTML and compare with published 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 sent, 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 that it can also track the difference in the keys of the array, and not just the values, for example, if b and c are placed (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) 

therefore, unpublished 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 
+1
source

The simplest workaround is to use hidden input as the value = 0, for example, it has the same name as the flag after it.

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

0
source

IF you do not want to use JS - you could do it

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

That way you always get $ _POST for the checkbox value

0
source

Source: https://habr.com/ru/post/1490749/


All Articles