List checkbox from database using php and ajax

1

I need to show a checkbox listing from a database with ajax. I have an example that lists a select with data from the database but I can not find a way to modify it to show the checkbox .

The form:

<form role="form" id="agregar" name="agregar" method="post">
    <select name="usuario" id="usuario">
        <option value="" disabled selected>listado</option>
    </select>
    <button type="submit" >Enviar</button>
</form>

Ajax:

$select = $('#usuario');
$.ajax({
    url:"conexion.php",
    dataType:'JSON',
    success:function(response){
        $select.append(response.seleccionUsuario);
        $('select').formSelect();
    },
    error:function(){
        $select.html('<option id="-1">Error!</option>');
    }
});

The php file that I call from ajax:

$mysqli = new mysqli("localhost","root","","tabla");
if($mysqli->connect_errno) {
    echo "Falló al conectar".$mysqli->connect_errno;
} else {
    $mysqli->set_charset("utf8");
    $query = "SELECT * FROM usuario";
    $resultado = $mysqli->query($query);
    $jsondata['seleccionUsuario'] = '';
    while($row = mysqli_fetch_array($resultado)) {
        $jsondata['seleccionUsuario'] .= '<option name="usuario" id="usuario" value="'.$row["id"].'">'.$row["nombre_usuario"].'</option>';
    }
    header('Content-type: application/json; charset=utf-8');
    print_r(json_encode($jsondata));
}
exit();

The previous code lists N users of a database in select . I would like to list the same but in a checkbox.

    
asked by Johan Solano Contreras 31.08.2018 в 05:37
source

0 answers