Do not load the values in input

0

Good afternoon everyone, I'm pulling some data from the BD and I send them to javascript because the data pulls them as they see in the image, I do not know why the values are not assigned in the input. I appreciate your help.

$(function(){
	$.post("../php/cargar_centros.php", function(data){
		$("#centro").html(data);
	});
	$("#centro").change(function(){
		$.post("../php/buscar_centro.php",{centro: $("#centro").val()}, function(data){
			$("input").removeAttr('disabled');
			$("#nombre").val(data.centro);
			$("#descripcion").val(data.descripcion);
		});
	});
	$("#btn-enviar").click(function(){
		
	});
});
<?php
	include("conex.php");
	session_start();
	$centro = $_POST['centro'];
	$sql = "SELECT nombre, descripcion FROM centros_salidas WHERE id_centro = '$centro'";
	$result = mysqli_query($con, $sql);
	$row = mysqli_fetch_array($result);
	echo json_encode(array("centro"=>$row['nombre'], "descripcion" =>$row['descripcion']));
	mysqli_free_result($result);
	mysqli_close($con);
?>
<!DOCTYPE html>
<html lang = "es">
<head>
	<meta charset = "UTF-8">
	<title></title>
	<link rel="stylesheet" href="../css/reg.css">
	<script src = "../js/jquery-3.2.1.min.js"></script>
	<script src = "../js/alertas.js"></script>
	<script src = "../js/mod_centro.js"></script>
</head>
<body>
	<div class="contenedor">
		<form  method= "post" class = "form-registro">
			<h2> Modificar centro de salida</h2>
			<div class ="contenedor-input">
				<select name = "centro" id = "centro" class = "input-100">
				</select>
				<input type = "text" id = "nombre" name = "nombre" placeholder = "Centro de salida" class = "input-100" disabled = false/>
				<input type = "text" id = "descripcion"  name = "descripcion" placeholder = "Descripción" class = "input-100" disabled = false/>
				<input type = "button" value = " Modificar centro" class = "btn-enviar" id = "btn-enviar"/>
			</div>		
		</form>
	</div>
</body>
</html>

this is the result of the php

    
asked by Familia Valencia Hdz 24.01.2018 в 19:25
source

2 answers

1

Try changing data.centro by JSON.parse(data).centro and the same with the other data.

    
answered by 24.01.2018 / 20:29
source
1

The response of your PHP is returning a JSON effectively but I think you are missing the Header, so that the Browser looks like that to a JSON on the client side.

Add this line before the echo of your PHP

header('Content-Type: application/json');

That it serves you

    
answered by 24.01.2018 в 21:37