PHP - Problems loading data in modal Bootstrap

1

I'm doing a CRUD, but in the updating part it's giving me war.
I am using a modal, which is the following:

<h5 class="modal-title" id="exampleModalLabel">Actualizar Información</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
    <span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
    <form id="frmactualiza">
        <input type="text"  name="id_usuario" id="id_usuario">
        <label>Nombre Completo</label>
        <input type="text" class="form-control form-control-sm" name="nombrejU" id="nombrejU">
        <label>Categoria : </label><br>
        <input type="radio" name="niveljU" value="1">
        <label>Administrador</label>
        <br><input type="radio" name="niveljU" value="2">
        <label>Maestro</label><br>
        <input type="radio" name="niveljU" value="3">
        <label>Estudiante</label>
        <br><br><label>Direccion</label>
        <input type="text" class="form-control form-control-sm" name="direccionjU" id="direccionjU">
        <label>Telefono</label>
        <input type="text" class="form-control form-control-sm" name="telefonojU" id="telefonojU">
        <label>Celular</label>
        <input type="text" class="form-control form-control-sm" name="celularjU" id="celularjU">
        <label>Correo</label>
        <input type="text" class="form-control form-control-sm" name="correojU" id="correojU">
        <label>Contraseña</label>
        <input type="text" class="form-control form-control-sm" name="clavejU" id="clavejU">
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
    <button type="button" class="btn btn-raised btn-warning" id="btnactualizar">Actualizar</button>
</div>
</div>
</div>

Then I use this method to obtain the data:

<script type="text/javascript">
  function obtenDatos(idUsuario){
    $.ajax({
      type: "POST",
      data: "idUsuario=" + idUsuario,
      url: "php/obtenerRegistro.php",
      success: function(r){
        datos = jQuery.parseJSON(r);

        $('#id_usuario').val(datos['id_usuario']);
        $('#clavejU').val(datos['clavejU']);
        $('#niveljU').val(datos['niveljU']);
        $('#nombrejU').val(datos['nombrejU']);
        $('#direccionjU').val(datos['direccionjU']);
        $('#telefonojU').val(datos['telefonojU']);
        $('#celularjU').val(datos['celularjU']);
        $('#correojU').val(datos['correojU']);
      }
    });
  }

The sending of the variable is here:

<tr>
    <td><?php echo $ver[0]; ?></td>
    <td><?php echo $ver[1]; ?></td>
    <td><?php echo $ver[2]; ?></td>
    <td><?php echo $ver[3]; ?></td>
    <td><?php echo $ver[4]; ?></td>
    <td><?php echo $ver[5]; ?></td>
    <td><?php echo $ver[6]; ?></td>
    <td style="text-align: center;">
        <span class="btn btn-raised btn-warning btn-xs" onclick="obtenDatos('<?php echo $ver[0]; ?>')" data-toggle="modal" data-target="#updatemodal">
        <span class="fa fa-pencil-square-o"></span> Editar</span>
    </td>
    <td style="text-align: center;">
        <span class="btn btn-raised btn-danger btn-xs" 
            onclick="elimina('<?php echo $ver[0]; ?>')">
        <span class="fa fa-trash"></span> Eliminar</span>
    </td>
</tr>

And the update:

<?php
require_once "conexion.php";
$conexion        = conexion();
$id              = $_POST['id_usuario'];
$nombre_completo = $_POST['nombrejU'];
$nivel           = $_POST['niveljU'];
$direccion       = $_POST['direccionjU'];
$telefono        = $_POST['telefonojU'];
$celular         = $_POST['celularjU'];
$correo          = $_POST['correojU'];
$clave           = $_POST['clavejU'];

$sql = "CALL sp_actualizar_datos('$id','$clave','$nombre_completo','$nivel','$direccion','$telefono','$celular','$correo')";

echo mysqli_query($conexion, $sql);
?> 

The problem is that when editing gives me the model but with the empty fields, I use the following template:

GitHub Self-taught Faculty Template

Thanks in advance for your valuable help.

    
asked by Dennis 24.04.2018 в 00:58
source

2 answers

1

The error must be in the line

echo mysqli_query($conexion,$sql);

You can not do echo directly on what is returned by mysqli_query . First you have to convert the data to an array and from there to JSON.

$result = mysqli_query($conexion,$sql);
$json = json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));
$error = json_last_error();

header('Content-type: text/javascript');

if ( !$error ) {
    echo $json;
} else {
    echo "{'error':$error}";
}

Although it would be better to use an while and build an array with only the data you need and then do the conversion to JSON because depending on the content of the table and the encoding of it json_encode may fail.

Editing: Added coding error control and sending correct headers

    
answered by 24.04.2018 в 05:42
0

Possibly because of the way you access the attributes of an object (JSON)

datos['id_usuario'];  

try this way:

datos.id_usuario;
    
answered by 24.04.2018 в 02:19