How to pass data with json

0

I need to pass from PHP a JSON to each field that corresponds the value, for the moment I try with the value of select and you are supposed to put it in the cedula field. I attach the code

$(document).ready(function(){
	$.post("../php/llenar.php", function(data){
		$("#capitan").html(data);
	});
	$("#capitan").change(function(){
		
		alert($("#capitan").val());
		$.get("../php/modificar.php",{cedula: $("#capitan").val()},function(data){
			$("#cedula").val(data.cedula);
		},"json");
	});
});
<!DOCTYPE html>
<html lang = "es">
<head>
	<meta charset = "UTF-8">
	<title id="usuario"></title>
	<link rel="stylesheet" href="../css/reg.css">
	<script src = "../js/jquery-3.2.1.min.js"></script>
	<script src = "../js/modificar_Cap.js"></script>
</head>
<body>
	<form  method= "post" class = "form-registro">
		<h2> Modificar capitanes</h2>
		<div class ="contenedor-input">
			<select name = "capitan" id = "capitan" class = "input-100">
			</select>
			<input type = "text" id = "cedula" name = "cedula" placeholder = "Cedula" class = "input-100" disabled = false/>
			<input type = "text" id = "nombre"  name = "nombre" placeholder = "Nombres" class = "input-48" disabled = false/>
			<input type = "text" id = "apellido" name = "apellido" placeholder = "Apellidos" class = "input-48" disabled = false/>
			<input type = "email" id = "mail" name = "mail" placeholder = "Correo electronico" class = "input-100" disabled = false/>
			<input type = "text" id = "user" name = "user" placeholder = "Usuario" class = "input-48" disabled = false/>
			<input type = "text" id = "pwd" name = "pwd" placeholder = "Contraseña" class = "input-48" disabled = false/>
			<input type = "text" id = "privilegio" name = "privilegio" placeholder = "Cedula" class = "input-100" disabled = false/>
			<input type = "button" value = " Modificar capitan" class = "btn-enviar" id = "btn-enviar" disabled = false/>
	
		</div>		
	</form>
</body>
</html>

PHP

<?php
    $cedula = $_GET['cedula'];
    echo $cedula;
    echo json_encode(array("cedula"=>$cedula));
?>
    
asked by Familia Valencia Hdz 15.01.2018 в 00:03
source

1 answer

1

First remove the first echo of PHP echo $cedula; , you should only print json

$cedula = $_GET['cedula'];
echo json_encode(array("cedula"=>$cedula));

Then in your Jquery, it should be like this, with the replacement of ready of document in a more current way. Jquery3 . Since $(document).ready(function(){ is declared obsolete.

 $(function() {
     $("#capitan").change(function(){
        $.get("script.php",{cedula: $("#capitan").val()},function(data){
            $("#cedula").val(data.cedula);
        },"json");
     });
 });
    
answered by 15.01.2018 / 00:14
source