php enable table row

0

Good afternoon. the fact is that I have a table that is filled from a database and I need to place a checkbox that when I select it, I only enable that row, the php code of the table is:

<?php
    $sql = "Select * FROM usuarios, estado, roles Where estado.id_estado = usuarios.id_estado AND roles.id_rol = usuarios.id_rol";
    $result = mysqli_query($conexion,$sql);
    echo "
        <br>
        <br>
        <table id='usuarios' border = 1 cellspacing = 1 cellpadding = 1>
            <tr>
                <th>Identificacion</th>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>Telefono</th>
                <th>Direccion</th>
                <th>Fecha de registro</th>
                <th>Estado</th>
                <th>Rol</th>
            </tr>";
    while($row = mysqli_fetch_array($result)){
        echo "
            <tr class='item'>
                <td>
                    <input name='ident' class='cajas' type='text' id='ident' value=" . $row['identificacion'] . " disabled>
                </td>" .
                "<td>
                    <input name='nombre' class='cajas' type='text' id='nombre' value=" . $row['nombre'] . " disabled>
                </td>" .
                "<td>
                    <input name='apellido' class='cajas' type='text' id='apellido' value=" . $row['apellido'] . " disabled>
                </td>" .
                "<td>
                    <input name='telefono' class='cajas' type='text' id='telefono' value=" . $row['telefono'] . " disabled>
                </td>" .
                "<td>
                    <input name='direccion' class='cajas' type='text' id='direccion' value=" . $row['direccion'] . " disabled>
                </td>" .
                "<td>
                    <input name='fecha' class='cajas' type='text' id='fecha' value=" . $row['fecha_registro'] . " disabled>
                </td>" .
                "<td>
                    <input name='estado' class='cajas' type='text' id='estado' value=" . $row['estado'] . " disabled>
                </td>" .
                "<td> 
                    <input name='rol' class='cajas' type='text' id='rol' value=" . $row['tipo'] . " disabled>
                </td>" .
                "<td>
                    <input type='checkbox'>
                </td>" .
            "</tr>";
    }
    echo "</table>";
?>

I do not know if it's possible with a JS function, I tried with the OnClick property in the check but it does not work for me

Thank you very much

    
asked by Nestor Bautista 02.01.2018 в 20:10
source

2 answers

0

Good, it can be done with the onchange event, it can be done in several ways, maybe it is not the most correct way but I tried to affect as little as possible your code.

Basically this function what it does is to find if the disabled exists in the in the syntax of the father tr, and depending on whether it finds it or not, it introduces it or crushes it.

function changeDisable(e) {
  const parent =  e.parentNode.parentNode;
  const text = parent.innerHTML;
  if (text.indexOf("disabled") !== -1) {
    parent.innerHTML = text.replace(new RegExp('disabled', 'g'), "");
    return;
  }
  parent.innerHTML  = text.replace(new RegExp('type="text"', 'g'), 'type="text" disabled');
}
<?php
    $sql = "Select * FROM usuarios, estado, roles Where estado.id_estado = usuarios.id_estado AND roles.id_rol = usuarios.id_rol";
    $result = mysqli_query($conexion,$sql);
    echo "
        <br>
        <br>
        <table id='usuarios' border = 1 cellspacing = 1 cellpadding = 1>
            <tr>
                <th>Habilitar</th>
                <th>Identificacion</th>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>Telefono</th>
                <th>Direccion</th>
                <th>Fecha de registro</th>
                <th>Estado</th>
                <th>Rol</th>
            </tr>";
    while($row = mysqli_fetch_array($result)){
        echo "
            <tr class='item'>
                <td>  <input type='checkbox' onchange='changeDisable(this)'> </td>
                <td>
                    <input name='ident' class='cajas' type='text' id='ident' value=" . $row['identificacion'] . " disabled>
                </td>" .
                "<td>
                    <input name='nombre' class='cajas' type='text' id='nombre' value=" . $row['nombre'] . " disabled>
                </td>" .
                "<td>
                    <input name='apellido' class='cajas' type='text' id='apellido' value=" . $row['apellido'] . " disabled>
                </td>" .
                "<td>
                    <input name='telefono' class='cajas' type='text' id='telefono' value=" . $row['telefono'] . " disabled>
                </td>" .
                "<td>
                    <input name='direccion' class='cajas' type='text' id='direccion' value=" . $row['direccion'] . " disabled>
                </td>" .
                "<td>
                    <input name='fecha' class='cajas' type='text' id='fecha' value=" . $row['fecha_registro'] . " disabled>
                </td>" .
                "<td>
                    <input name='estado' class='cajas' type='text' id='estado' value=" . $row['estado'] . " disabled>
                </td>" .
                "<td> 
                    <input name='rol' class='cajas' type='text' id='rol' value=" . $row['tipo'] . " disabled>
                </td>" .
                "<td>
                    <input type='checkbox'>
                </td>" .
            "</tr>";
    }
    echo "</table>";
?>
    
answered by 02.01.2018 / 21:22
source
-6

Well, I did something similar look if it serves you and accommodate what is yours.

Do it with an onchange by calling js.

You give him a name and you put him onchange

name="ejemplo" onchange="cambia_visibles(this)"
function cambia(elemento) {
    if (elemento.value === "checked") {
        document.getElementById("ejemplo").disabled = "true";
    } else {
        document.getElementById("ejemplo").disabled = "false";
    }
}
    
answered by 02.01.2018 в 20:22