Load data in an Input when selecting the data of other input

1

I need to select the document number of an Input from a list that is displayed, load the name to which that document number belongs in another Input. Example, when selecting the document number 12345, load me in another input, for example Pepito.

====ajax.php===
<?php
	$dbHost = 'localhost';
    $dbUsername = 'root';
    $dbPassword = 'root';
    $dbName = 'db_personas';

    $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

    $searchTerm = $_GET['term'];
    $query = $db->query("SELECT * FROM persona WHERE documento LIKE '%".$searchTerm."%' ORDER BY documento ASC");
    while ($row = $query->fetch_assoc()) {
        $data[] = $row['documento'];
    }
    echo json_encode($data);
?>
====index.php====
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    </head>
    <body>
        <div style="width:520px;margin:0px auto;margin-top:30px;height:500px;">
           
            <div class="col-md-6">
        <div class="form-group">
            <input id="documento" class="form-control" type="text" placeholder="Ingrese Número de Documento">
        </div>
        </div>
            <br>
           <div class="col-md-6">
        <div class="form-group">
            <input id="nombres" class="form-control" type="text">
        </div>
        </div>
        </div>
        
   <script type="text/javascript">
        $(function() {
            $( "#documento").autocomplete({
                source: 'ajax.php'
            });
        });
    </script>
    </body>
</html>
    
asked by 05.08.2017 в 02:26
source

1 answer

0

index.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ejemplo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script type="text/javascript">
$(function() {
            $("#document").autocomplete({
                source: "persona.php",
                minLength: 2,
                select: function(event, ui) {
					event.preventDefault();
					$('#document').val(ui.item.document);
					$('#nombr').val(ui.item.nombr);
					$('#apellid').val(ui.item.apellid);
					$('#nacimient').val(ui.item.nacimient);
					$('#id_persona').val(ui.item.id_persona);
			     }
            });
		});
</script>
</head>
<body>
 
<div class="ui-widget">
  Documento:  <input id="document">
  Nombres: <input id="nombr" readonly>
  Apellidos: <input id="apellid" readonly>
  Nacimiento: <input id="nacimient" readonly>
  <input type="hidden" id="id_persona">
</div>
</body>
</html>

persona.php

<?php
if (isset($_GET['term'])){
	# conectare la base de datos
    $con=@mysqli_connect("localhost", "root", "root", "nombre_db");
	
$return_arr = array();
/* Si la conexión a la base de datos , ejecuta instrucción SQL. */
if ($con)
{
	$fetch = mysqli_query($con,"SELECT * FROM persona where documento like '%" . mysqli_real_escape_string($con,($_GET['term'])) . "%' LIMIT 0 ,50"); 
	
	/* Recuperar y almacenar en conjunto los resultados de la consulta.*/
	while ($row = mysqli_fetch_array($fetch)) {
		$id_persona=$row['id_persona'];
		$row_array['value'] = $row['documento']." | ".$row['nombres']." ".$row['apellidos'];
		$row_array['id_persona']=$row['id_persona'];
		$row_array['document']=$row['documento'];
		$row_array['nombr']=$row['nombres'];
		$row_array['apellid']=$row['apellidos'];
		$row_array['nacimient']=$row['nacimiento'];
		array_push($return_arr,$row_array);
    }
}

/* Cierra la conexión. */
mysqli_close($con);

/* Codifica el resultado del array en JSON. */
echo json_encode($return_arr);

}
?>

Note that in each of the sections $ row ['names']; what goes inside the [] is the name of the attributes that you have in the database.

    
answered by 12.08.2017 / 15:05
source