0

Tengo un formulario que me guarda checkbox y quisiera que al momento de querer editar me aparezcan los checkbox que había seleccionado anteriormente Ejemplo:

introducir la descripción de la imagen aquí
el código del formulario editar es el siguiente:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
    include "conexion/db.php";
    $id = $_GET['Id_situ'];
    $sam = "SELECT * FROM estado
    where  Id_situ = '$id'";
    $sal = $cone->query($sam);
    $row = $sal->fetch_assoc();
    ?>

    <form>
        <center>
            <h1>Pruebaaaa</h1><br><br>

            <input type="checkbox" name="" id="act" value="<?php if($row['Soltero'] == "Si"){ echo 'Checked: '.checked.''; }?>" >
            <label>Soltero</label for = "act">

                <input type="checkbox" name="" id="act2" value="<?php if($row['Casado'] == "Si"){ echo 'Checked: '.checked.''; }?>" >
                <label>Casado</label for = "act2">

                    <input type="checkbox" name="" id="act3" value="<?php if($row['Uni'] == "Si"){ echo 'Checked: '.checked.''; }?>" >
                    <label>Union libre</label for = "act3">
        </center>
    </form>

</body>
</html>
1
  • Por favor, indícanos si esta respuesta ha aclarado tu duda y de qué forma podemos ampliarla para que también pueda ayudar a otras personas en tu misma situación. Has hecho un esfuerzo planteando la pregunta, no lo dejemos a medio camino. Recuerda validar y votar y Gana +2 de reputación
    – David JP
    el 6 ene. 2020 a las 18:33

1 respuesta 1

1

Ok, ya he visto el error.

Tú intentas hacer algo así:

<input type="checkbox" value="checked">

Cuando en realidad se hace así:

<input type="checkbox" checked>

Te paso el dataset y el código en PHP para que puedas hacer las comprobaciones necesarias:

CREATE TABLE estado(
  Id_situ int AUTO_INCREMENT PRIMARY KEY,
  Soltero varchar(2),
  Casado varchar(2),
  Uni varchar(2)
  );
INSERT INTO estado (Soltero, Casado, Uni) VALUES
  ('No','No','No'),
  ('No','','No'),
  ('No','No','No'),
  ('','No','No'),
  ('No','Si','No'),
  ('No','No','Si');

Código en PHP con algunas modificaciones para realizar la depuración:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
    $cone = new mysqli('localhost','root','','stackoverflow');
    //include "conexion/db.php";
    if(isset($_GET['Id_situ'])){
        $id = $_GET['Id_situ'];
        $sam = "SELECT * FROM estado
        where  Id_situ = '$id'";
        $sal = $cone->query($sam);
        $row = $sal->fetch_assoc();
    ?>

    <form>
        <center>
            <h1>Pruebaaaa</h1><br><br>

            <input type="checkbox" name="" id="act" <?php if($row['Soltero'] == "Si"){ echo 'Checked'; }?> >
            <label>Soltero</label for = "act">

            <input type="checkbox" name="" id="act2" <?php if($row['Casado'] == "Si"){ echo 'Checked'; }?> >
            <label>Casado</label for = "act2">

            <input type="checkbox" name="" id="act3" <?php if($row['Uni'] == "Si"){ echo 'Checked'; }?> >
            <label>Union libre</label for = "act3">

            <?php
            }
                $r=$cone->query("
                    SELECT * FROM estado;
                ")->fetch_all(MYSQLI_ASSOC);
                $txt='<table border>';
                foreach($r as $l){
                    $txt.='<tr>';
                    foreach($l as $campo=>$c){
                        $txt.='<td>'.$c.'</td>';
                        if($campo=='Uni')
                            $txt.='<td><a href="?Id_situ='.$l['Id_situ'].'">Agregar</a></td>';
                    }
                    $txt.='</tr>';
                }
                $txt.='<table>';
                echo $txt;

            ?>

        </center>
    </form>
</body>
</html>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.