Loading data with JSON shows nothing

1

I want to show values with JSON when collecting information from two text fields.

Here the codes:

HTML:

 <input type="text" name="valor_grados1" id="valor_grados1">Superior
 <input type="text" name="valor_grados" id="valor_grados">Novato

 <tr>
 <td>Seleccione el tipo de castigo: </td>
 <td><select onchange="obtenerAmplitud();">
 <?php
 castigos();
 ?>
 </select>
 </td>
 </tr>

 <td> Dias Simples:
      <input type="text" name="valor_amplitud1" maxlength="2" size="2" id="valor_amplitud1">
      <br>
      Dias Severos:
      <input type="text" name="valor_amplitud2" maxlength="2" size="2" id="valor_amplitud2">

getAmplitude ()

 function obtenerAmplitud() {
var gsan2= { gsancionador: $('#valor_grados1').val() }
alert(gsan2);
var gsan1= { gsancionado: $('#valor_grados').val() }
alert(gsan1);

$.post("./funciones_php_obtenerAmplitud.php", gsan2, gsan1 , function( respuesta ) 
{
    $('#valor_amplitud1').val(respuesta.simple);
    $('#valor_amplitud2').val(respuesta.severo);

}, "json" );

}

get.php

 $grado_sancionador = $_POST["gsancionador"];

  $grado_sancionado = $_POST["gsancionado"];


   //-- Obtener datos basicos de amplitud --//
  $stmt=$conexion->prepare("SELECT amplitud_dias_simple,amplitud_dias_severo FROM demeritos WHERE grado_sancionador_id=? AND grado_sancionado_id=?");
  $stmt->bind_param("ii", $grado_sancionador,$grado_sancionado);
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($amplitud_simple,$amplitud_severo);
  $stmt->fetch();
   //-- ---------------------------- --//


 //-- Llenamos el JSON --//
       echo json_encode(array(
        'simple' => $amplitud_simple,
        'severo' => $amplitud_severo
           ));
      $stmt->close();
         }

CONSOLE error

Uncaught TypeError: (o.dataType || "*").toLowerCase is not a function
    at Function.ajax (http://localhost/sistema/jquery-3.1.1.min.js:4:12140)
    at Function.r.(anonymous function) [as post] (http://localhost/sistema/jquery-3.1.1.min.js:4:14657)
    at obtenerAmplitud (http://localhost/sistema/obtenerDatosBasicos.js:7:3)
    at HTMLSelectElement.onchange (http://localhost/sistema/menu.php:364:61)

What error does 7: 3 have?

Does not show data

    
asked by Victor Alvarado 13.03.2017 в 20:27
source

2 answers

1

The $.post signature is:

<url>, <data>, <successCb>, <dataType>

So, why do you pass two objects instead of just one? The request you make must be:

let data = {
  gsancionador: $('#valor_grados1').val(),
  gsancionado: $('#valor_grados').val()
};

$.post("./funciones_php_obtenerAmplitud.php", data , function( respuesta ) {
    $('#valor_amplitud1').val(respuesta.simple);
    $('#valor_amplitud2').val(respuesta.severo);
}, "json");

The error is related to JSON; it is possible that by having a parameter of more, the callback is interpreted as dataType and jQuery is not able to apply a correct parser.

    
answered by 13.03.2017 / 23:48
source
1

I hope this works for you.

<label for="clave">Clave</label>
<input type="password" name="clave" id="clave" tabindex="1" autofocus />   

<label for="nombre">valor1</label>
<input  type="text" name="valor1" id="valor1" readonly="readonly" />

I have the inptu that will perform the action that has as id calve and the input that will receive the answer with the id value1 you call the function when losing the focus the input with the id key.

$(document).ready(function(){
    $("#clave" ).focusout(function(){
            $.ajax({
                url:'obtener.php',
                type:'POST',
                dataType:'json',
                data:{clave:$('#clave').val()}
            }).done(function(respuesta){
                $("#valor1").val(respuesta.valor1);
         });
      });
    });

and in your php file you make the query and return the values.

$clave = $_POST['clave'];

$consulta = "SELECT * FROM tabla WHERE campo = '$clave'";
$result = $conexion->query($consulta);

$respuesta = new stdClass();
if($result->num_rows > 0){
    $fila = $result->fetch_array();
    $respuesta->nombre = $fila['valor1'];

}
echo json_encode($respuesta);
    
answered by 13.03.2017 в 23:13