PHP - repetir autocompletado php ajax jquery-ui

 
Vista:

repetir autocompletado php ajax jquery-ui

Publicado por José (3 intervenciones) el 08/10/2019 21:40:49
Buena tarde comunidad de LWP, estoy haciendo un proyecto en el que se autocompletan los inputs con la información que obtienen mediante ajax de la base de datos. Hasta ahora e podido hacer que agrege y elimine los renglones que necesite el usuario. Tambien se puede buscar y seleccionar los datos del input clave, pero aqui es en donde tengo mi fallo, es decir, no se autocompletan los demas inputs con los datos que le manda la base de datos.
Algún maestro que ilumine a este joven párvulo? Dejo los archivos que utilizo.

proyecto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!doctype html>
<html>
<head>
 <link rel="stylesheet" href="jquery-ui.min.css">
 <script src="jquery-3.4.1.min.js"></script>
 <script src="jquery-ui.min.js"></script>
 <script src="all.js"></script>
 <link rel="stylesheet" href="bootstrap.min.css">
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
<body>
 <br />
 <div class="container">
  <h3 align="center">proyecto</h3>
  <br />
  <h4 align="center">Introduce la clave</h4>
  <br />
  <form method="post" id="insert_form">
   <div class="table-responsive">
    <span id="error"></span>
    <table class="table table-bordered" id="item_table">
     <tr>
      <th>Clave</th>
      <th>Producto</th>
      <th>Piezas</th>
      <th>Lote</th>
      <th><button type="button" name="add" class="btn btn-success btn-sm add"><i class="fas fa-plus-circle"></i>Agregar</button></th>
     </tr>
    </table>
    <div align="center">
    <button type="submit" name="submit" class="btn btn-primary"><i class="fas fa-recycle"></i>Actualizar</button>
    </div>
    </div>
    </form>
    </div>
   </body>
</html>
 
<script>
$(document).ready(function(){
	$(document).on('click', '.add', function(){
		var html='';
		html += '<tr>';
		html += '<td><input id="clave" onkeyup="javascript:this.value=this.value.toUpperCase();"></td>';
		html += '<td><input id="producto" readonly></td>';
		html += '<td><input id="piezas" readonly></td>';
		html += '<td><input id="lote" readonly></td>';
		html += '<input type="hidden" id="id">';
		html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><i class="fas fa-minus-circle"></i>Eliminar</button></td></tr>';
		$('#item_table').append(html);
	});
 
	$(document).on('click', '.remove', function(){
		$(this).closest('tr').remove();
	});
 
	$(function() {
            $("#clave").autocomplete({
                source: "ajax.php",
                minLength: 1,
                select: function(event, ui) {
					event.preventDefault();
                    $('#clave').val(ui.item.clave);
					$('#producto').val(ui.item.producto);
					$('#piezas').val(ui.item.piezas);
					$('#lote').val(ui.item.lote);
					$('#id').val(ui.item.id);
			     }
            });
		});
});
</script>


ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 if (isset($_GET['term'])){
	# conectare la base de datos
    $con=@mysqli_connect("localhost", "root", "", "inventario");
 
$return_arr = array();
/* Si la conexión a la base de datos , ejecuta instrucción SQL. */
if ($con)
{
	$fetch = mysqli_query($con,"SELECT id, clave, producto, piezas, lote FROM existencias WHERE clave LIKE '%". mysqli_real_escape_string($con,($_GET['term']))."%' OR producto LIKE '%". mysqli_real_escape_string($con,($_GET['term']))."%'");
 
	/* Recuperar y almacenar en conjunto los resultados de la consulta.*/
	while ($row = mysqli_fetch_array($fetch)) {
		$id=$row['id'];
		$row_array['value'] = $row['clave']." | ".$row['producto']." | ".$row['piezas']." | ".$row['lote'];
		$row_array['id']=$row['id'];
		$row_array['clave']=$row['clave'];
		$row_array['producto']=$row['producto'];
		$row_array['piezas']=$row['piezas'];
		$row_array['lote']=$row['lote'];
		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);
 
}
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder