Problem with AJAX

1

I'm working on a code, but I'm taking tests to be able to load information from the database into two fields, from the selection of a select. The problem is that when executing the ajax it shows the error. Now execute the code where the query is created for ajax to return it and execute it perfectly. I leave my code here

  function mifuncion(valor) {
    $.ajax({

      url: 'proceso.php',


      data: {
        valor: valor
      },


      type: 'POST',


      dataType: 'json',

      success: function(json) {

        $("#NombreCliente").value = json.nombre;
        $("#CodigoCliente").value = json.codigo;
      },


      error: function(xhr, status, errorThrown) {
        alert('Disculpe, existió un problema');
      }
    });
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>



<label>Cliente: </label>
<select name="valor" required onchange='mifuncion(this.value)'>
  <option value="">Seleccionar</option>
  <?php
        
        require("../conexion.php");
                     
        $var_consulta= "SELECT * FROM tpersonal ";
        $var_resultado = $mysqli->query($var_consulta);

        if($var_resultado->num_rows>0){

        while ($var_fila=$var_resultado->fetch_array()){
        
        echo "<option  value=".$var_fila['TPER_IDPER'].">".$var_fila['TPER_NOMBR']."</option>";}}
    ?>
</select>

<label>Código: </label>
<input name="CódigoCliente" id="CodigoCliente" type="text">

<label>Nombre: </label>
<input name="NombreCliente" id="NombreCliente" type="text">

<input name="reset" value=" Limpiar " type="reset" />

<input name="enviar" value="Registrar" type="submit" />

</form>

<!----------------------- ARCHIVO AJAX.PHP --------------------------------->
<?php
require("../conexion.php");

$valor=$_POST['valor'];

 

$jsondata = array();
 

$consulta="Select TPER_CARGO,TPER_NOMBR from tpersonal where TPER_IDPER='$valor'";
 
$resultado = $mysqli->query($consulta);
$resultados = mysqli_fetch_array($resultado, MYSQLI_ASSOC); 
 
$nombre=$resultados['TPER_CARGO'];
$codigo=$resultados['TPER_NOMBR'];
 

$jsondata['TPER_CARGO'] = $nombre;
$jsondata['TPER_NOMBR'] = $codigo;
 

 header('Content-type: application/json; charset=utf-8');
 echo json_encode($jsondata);
 
?>
    
asked by Erik Raúl González Páez 31.08.2018 в 21:11
source

2 answers

0

Finally this is the code that with the help of A. Cedano and Andres Martinez manage to solve

function mifuncion(valor) {
    $.ajax({

      url: '../pruebas/proceso.php',


      data: {
        valor: valor
      },


      type: 'POST',


      dataType: 'json',

      success: function(json) {

$("#CodigoCliente").val(json.codigo);
$("#NombreCliente").val(json.nombre);
      },


      error: function(xhr, status, errorThrown) {
        alert('Disculpe, existió un problema');
      }
    });
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>



<label>Cliente: </label>
<select name="personal" required onchange='mifuncion(this.value)'>
  <option value="">Seleccionar</option>
  <?php
        
        require("../conexion.php");
                     
        $var_consulta= "SELECT * FROM tpersonal ";
        $var_resultado = $mysqli->query($var_consulta);

        if($var_resultado->num_rows>0){

        while ($var_fila=$var_resultado->fetch_array()){
        
        echo "<option  value=".$var_fila['TPER_IDPER'].">".$var_fila['TPER_NOMBR']."</option>";}}
    ?>
</select>

<label>Código: </label>
<input name="CodigoCliente" id="CodigoCliente" type="text">

<label>Nombre: </label>
<input name="NombreCliente" id="NombreCliente" type="text">

<input name="reset" value=" Limpiar " type="reset" />

<input name="enviar" value="Registrar" type="submit" />

</form>

<?php
require("../conexion.php");

$valor=$_POST['valor'];

 

$jsondata = array();
 

$consulta="Select TPER_CARGO,TPER_NOMBR from tpersonal where TPER_IDPER='$valor'";
 
$resultado = $mysqli->query($consulta);
$resultados= mysqli_fetch_array($resultado);
 
$nombre=$resultados['TPER_CARGO'];
$codigo=$resultados['TPER_NOMBR'];
 

$jsondata['nombre'] = $nombre;
$jsondata['codigo'] = $codigo;
 

 header('Content-type: application/json; charset=utf-8');
 echo json_encode($jsondata);
 
?>
    
answered by 31.08.2018 / 23:35
source
0
$("#NombreCliente").value = json.nombre; 

change it to:

$("#NombreCliente").val(json.nombre);

and so on for all the values you want to load.

If that is not the problem, it shows that it appears on the console.

    
answered by 31.08.2018 в 21:16