How to know which CheckBox were selected To be able to Update

2

Currently I am working on a pop-up window and I have it in the following way

It is assumed that this is the update view, for which said user has already registered his information before, now I would like to know how I do it so that in my checkbox I can check all the ones that he selected when registering. At the moment I do not have a written code because I have not known how to start.

This opens the pop-up window:

function alerta() {
                new $.Zebra_Dialog('<strong>Actualizacion del Usuario</strong><br><br>', {
                    'source': {'ajax': 'perfil/actualizar.php?nickP=<?php echo "$nick2" ?>'},
                    width: 1000,
                    buttons: false,
                    animation_speed_show: 3000,
                    animation_speed_hide: 3000,
                    'title': 'Actualizar'
                });
            }

And the form is currently in this way:

<body>
        <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "proyecto_terror";

        $conn = mysqli_connect($servername, $username, $password, $dbname);

        $nick2 = $_GET["nickP"];
        $sql = "SELECT * FROM usuario WHERE nick = '" . $nick2 . "'";
        $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
        $datos = array();
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                $datos[] = $row;
                ?>


                <div id="alerta" style="width: 100%;height: 100%; ">

                    <form id="formularin" method="POST" action="perfil/Update.php"><center><table  class="formulario"  id="tabla" >
                                <tr>

                                    <td><center><label >Nombre: </label></center></td>
                                <td><input onkeypress="sololetras()" id="nombre" class="cajaTexto" value="<?php echo $row["nombre"]; ?>" name="nombre" type="text"></td>

                                </tr>
                                <tr>
                                    <td><center><label >Nick:</label></center></td>
                                <td><input maxlength="20"  id="nick" class="cajaTexto" value="<?php echo $row["nick"]; ?>" name="nick" type="text"></td>
                                </tr>
                                <tr>
                                    <td><center>Cumpleaños: </center></td>
                                <td>
                                    <span style="margin-left: 20px;margin-right: 10px;"> Dia: </span> <select name="dia" class="selector" id="dia">
                                        <option value="01">1</option>
                                        <option value="02">2</option>
                                        <option value="03">3</option>
                                        <option value="04">4</option>
                                        <option value="05">5</option>
                                        <option value="06">6</option>
                                        <option value="07">7</option>
                                        <option value="08">8</option>
                                        <option value="09">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                        <option value="26">26</option>
                                        <option value="27">27</option>
                                        <option value="28">28</option>
                                        <option value="29">29</option>
                                        <option value="30">30</option>
                                    </select>

                                    <span style="margin-left: 20px;margin-right: 10px;"> Mes: </span><select name="mes" class="selector" id="mes">
                                        <option value="01">Enero</option>
                                        <option value="02">Febrero</option>
                                        <option value="03">Marzo</option>
                                        <option value="04">Abril</option>
                                        <option value="05">Mayo</option>
                                        <option value="06">Junio</option>
                                        <option value="07">Julio</option>
                                        <option value="08"> Agosto</option>
                                        <option value="09">Septiembre</option>
                                        <option value="10">Octubre</option>
                                        <option value="11">Noviembre</option>
                                        <option value="12">Diciembre</option>

                                    </select>
                                    <span style="margin-left: 20px;margin-right: 10px;"> Año: </span><input name="ano" class="spinnerT" id="ano"  TYPE="NUMBER" MIN="1950" MAX="2006" STEP="1" VALUE="2000">
                                </td>
                                </tr>
                                <tr>
                                    <td><center><label >Edad: </label></center></td>
                                <td><input class="spinnerT" value="<?php echo $row["edad"]; ?>" id="edad" name="edad"  TYPE="NUMBER" MIN="10" MAX="66" STEP="1" VALUE="20"></td>
                                </tr>
                                <tr>
                                    <td><center><label >Sexo: </label> </center></td>
                                <td><div class="radio"> 
                                        <?php
                                        if ($row["sexo"] == "hombre") {
                                            ?>
                                            <input type = "radio" name = "sexo" value = "hombre" checked = "checked" id = "hombre">
                                            <label for = "hombre" >Hombre</label>
                                            <input type="radio" name="sexo" value="mujer" id="mujer">        
                                        <label for="mujer">Mujer</label>  
                                            <?php
                                        } else {
                                ?>
                                                  <input type = "radio" name = "sexo" value = "hombre"  id = "hombre">
                                            <label for = "hombre" >Hombre</label>
                                        <input type="radio" name="sexo" value="mujer" checked = "checked" id="mujer">        
                                        <label for="mujer">Mujer</label>                  
<?php
                                        }
                                        ?>

                                    </div>                      
                                    </tr>
                                <tr>
                                    <td><center><label >Intereses: </label></center></td>
                                <td>
                                    <div class="checkbox">
                                        <table >

                                            <tr>
                                                <td>                        <input  value="Asesinos"  type="checkbox" id="checkbox1"  name="checkbox[]"><label for="checkbox1" style="margin-right: 10px;">Asesinos</label>
                                                </td>
                                                <td>                        <input value="Misterios" type="checkbox" id="checkbox2"  name="checkbox[]" ><label for="checkbox2" style="margin-right: 30px;">Misterios</label>
                                                </td>
                                                <td>                        <input value="Creppys" type="checkbox" id="checkbox3"  name="checkbox[]"  ><label for="checkbox3" style="margin-right: 10px;">Creppys</label>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>                        <input value="Ovnis" type="checkbox" id="checkbox4"  name="checkbox[]"  ><label for="checkbox4" style="margin-right: 35px;">Ovnis</label>
                                                </td>
                                                <td>                        <input value="Enfermedades" type="checkbox" id="checkbox5"  name="checkbox[]"  ><label for="checkbox5" style="margin-right: -10px;">Enfermedades</label>
                                                </td>
                                                <td>                        <input value="Leyendas" type="checkbox" id="checkbox6"  name="checkbox[]" ><label for="checkbox6" style="margin-right: 10px;">Leyendas</label>
                                                </td>
                                            </tr>


                                            <tr>
                                                <td>                        <input value="Fantasmas" type="checkbox" id="checkbox7"  name="checkbox[]"  ><label  for="checkbox7" style="margin-right: -5px;">Fantasmas</label>
                                                </td>
                                                <td>                        <input value="Conspiraciones" type="checkbox" id="checkbox8"  name="checkbox[]"  ><label for="checkbox8" style="margin-right: -13px;">Conspiraciones</label>
                                                </td>
                                                <td>                        <input value="Deep-Web" type="checkbox" id="checkbox9"  name="checkbox[]" ><label for="checkbox9" style="margin-right: 10px;">Deep Web</label>
                                                </td>
                                            </tr>

                                        </table>
                                        <br>
                                    </div>
                                </td>
                                </tr>
                                <tr>
                                    <td colspan="2"><center><button id="btnsave"  style="margin-right: 20px;text-decoration: none;">Guardar<img src="../Imagenes/images/ICO/Mask 2.ico" style="width:20px;height: 20px;margin-top: -1px;margin-left: 3px;"></button>
                                    <button type="reset" id="btncancelar" >Cancelar<img src="../Imagenes/images/ICO/Jack O Lantern.ico" style="width:20px;height: 20px;margin-top: -1px;float: right;margin-right: 5px;"></button></center></td>

                                </tr>
                            </table></center></form>

                    <?php
                }
            }
            mysqli_close($conn);
            ?> 
        </div>

    </body>

In this way the interest is saved, which in this case would be the checkbox :

   if ($_POST['checkbox'] != "") {
        if (is_array($_POST['checkbox'])) {
            while (list($key, $value) = each($_POST['checkbox'])) {
                if ($contador == 0) {
                    $registro1 = $_POST['checkbox'][0];
                    $registro2 = "";
                    $registro3 = "";
                    $registro4 = "";
                    $registro5 = "";
                    $registro6 = "";
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 1) {
                    $registro2 = $_POST['checkbox'][1];
                    $registro3 = "";
                    $registro4 = "";
                    $registro5 = "";
                    $registro6 = "";
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 2) {
                    $registro3 = $_POST['checkbox'][2];
                    $registro4 = "";
                    $registro5 = "";
                    $registro6 = "";
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 3) {
                    $registro4 = $_POST['checkbox'][3];
                    $registro5 = "";
                    $registro6 = "";
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 4) {
                    $registro5 = $_POST['checkbox'][4];
                    $registro6 = "";
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 5) {
                    $registro6 = $_POST['checkbox'][5];
                    $registro7 = "";
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 6) {
                    $registro7 = $_POST['checkbox'][6];
                    $registro8 = "";
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 7) {
                    $registro8 = $_POST['checkbox'][7];
                    $registro9 = "";
                    $contador++;
                } else if ($contador == 8) {
                    $registro9 = $_POST['checkbox'][8];
                }
            }
        }

$cadena = $registro1 . " " . $registro2 . " " . $registro3 . " " . $registro4 . "  " . $registro5 . " " . $registro6 . " " . $registro7 . " " . $registro8 . " " . $registro9;


mysql_query("insert into usuario(nombre,nick,password,fecha_nac,edad,sexo,intereses,fecha_reg) values ('$nombre','$nick','$password','$cumpleano','$edad','$sexo','$cadena','$fechaR')");

And that's how they are stored in the database:

Now there is only one interest, but if you add more, they are assigned to the same field of the bd and you stop with a brief script.

Here you can see how my options appear in my checkbox :

Here you can see the interests saved:

There is no coincidence but in one and when I convert the explode in imploder this comes to me:

This is how I do the function in each checkbox :

<tr>
    <td><center><label >Edad: </label></center></td>
    <td><input class="spinnerT" value="<?php echo $row["edad"]; ?>" id="edad" name="edad"  TYPE="NUMBER" MIN="10" MAX="66" STEP="1" VALUE="20"></td
</tr>
    
asked by David 15.08.2016 в 02:52
source

3 answers

1

From the last updates, I can see that the table contains an "interests" field, with which the interest or interests selected will be in the variable $row["intereses"] . So what you could do is the following:

  • Create a new variable that will be an array created from $row["intereses"] (separating by , using explode )
  • When the checkbox is reached:
  • Check if the value is in the array created in point 1 (with array_search ).
  • If it is, check the box
  • The code that would be added to each checkbox would be:

    <?php if (array_search( [VALOR_DEL_CHECKBOX], $intereses) >= 0) { echo "checked"; } ?>
    

    This would be something like that (I only put the relevant part and I'm not sure I could prove it, it may contain some flaw):

    <?php
    
    $intereses = explode(",", $row["intereses"]);
    ?>
    
    <tr>
      <td><input  value="Asesinos"  type="checkbox" id="checkbox1"  name="checkbox[]" <?php if (array_search("Asesinos", $intereses) >= 0) { echo "checked"; } ?>><label for="checkbox1" style="margin-right: 10px;">Asesinos</label>
      </td>
      <td><input value="Misterios" type="checkbox" id="checkbox2"  name="checkbox[]" <?php if (array_search("Misterios", $intereses) >= 0) { echo "checked"; } ?>><label for="checkbox2" style="margin-right: 30px;">Misterios</label>
      </td>
      <td><input value="Creppys" type="checkbox" id="checkbox3"  name="checkbox[]" <?php if (array_search("Creppys", $intereses) >= 0) { echo "checked"; } ?> ><label for="checkbox3" style="margin-right: 10px;">Creppys</label>
      </td>
    </tr>
    
    <tr>
      <td><input value="Ovnis" type="checkbox" id="checkbox4"  name="checkbox[]"  <?php if (array_search("Ovnis", $intereses) >= 0) { echo "checked"; } ?>><label for="checkbox4" style="margin-right: 35px;">Ovnis</label>
      </td>
      <td><input value="Enfermedades" type="checkbox" id="checkbox5"  name="checkbox[]" <?php if (array_search("Enfermedades", $intereses) >= 0) { echo "checked"; } ?> ><label for="checkbox5" style="margin-right: -10px;">Enfermedades</label>
      </td>
      <td><input value="Leyendas" type="checkbox" id="checkbox6"  name="checkbox[]" <?php if (array_search("Leyendas", $intereses) >= 0) { echo "checked"; } ?> ><label for="checkbox6" style="margin-right: 10px;">Leyendas</label>
      </td>
    </tr>
    
        
    answered by 15.08.2016 / 05:28
    source
    0

    The last contribution helped me a lot, but it marks all of them except the last one, which can be due?

    $opt = explode(',', $cods);
    foreach ($res as $row) {
    	$checked = '';
    	if (array_search($row->codigo, $opt) > 0) $checked = 'checked';
    	$msg .= '<div class="form-check">
    	<input class="form-check-input" type="checkbox" name="equip_cubierta[]" id="equip_cubierta' . $row->codigo . '" value="' . $row->codigo . '" ' . $checked . '>
    	 <label class="form-check-label" for="equip_cubierta' . $row->codigo . '">' . $row->nombre . '</label>
    </div>';
    }
        
    answered by 09.07.2018 в 08:57
    -1

    Instead of

    <?php if (array_search("Misterios", $intereses) >= 0) { echo "checked"; } "Misterios"
    

    usa

    <?php if (array_search("Misterios", $intereses) > 0) { echo "checked"; } "Misterios"
    

    That is, remove the% sign = because having the unselected value "0" will be marked in the same way.

        
    answered by 02.09.2017 в 22:12