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);
?>