LLenar inputs con ajax
Publicado por dario (6 intervenciones) el 06/02/2020 15:11:28
Hola, quería saber si me podían ayudar con lo siguiente, tengo una base de datos en mysql, un html y ajax para darle mas dinamismo. Pude hacer botones de guardar, modificar y eliminar, pero en cuanto al botón buscar, me realiza la busqueda y me lo "imprime" en una hoja en blanco. Quiero hacer que esos valores se inserten en los inputs del formulario, para poder modificarlos. Dejo el código para ver si me ayudan a encontrar una solución, desde ya muchas gracias:
----------------------------ARCHIVO ajax.js:
---------------------ARCHIVO index1.php
----------------------ARCHIVO registro1.php (en este están todos los pedidos con el servidor, el conexion.php lo tengo aparte)
Muchas gracias!
----------------------------ARCHIVO ajax.js:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
window.onload = function () {
Cargar1()
}
function Registrar1()
{
var cost=$("#costo").val()
var product = $("#producto").val();
var preci = $("#precio").val();
var stoc = $("#stock").val();
// va a servir para radiobutton var tip = $("#tipo:checked").val();
$.ajax({
type: "POST",
dataType: 'html',
url: "registro1.php",
data: "costo="+cost+"&producto="+product+"&precio="+preci+"&stock="+stoc,
success: function(resp){
$('#respuesta').html(resp);
Limpiar1();
Cargar1();
}
});
}
function Cargar1()
{
$('#datagrid1').load('consulta1.php');
}
function Limpiar1(){
$("#costo").val("");
$("#producto").val("");
$("#precio").val("");
$("#stock").val("");
$("#id").val("");
}
function Eliminar1(){
var i=$("#id").val()
$.ajax({
type: "POST",
dataType: 'html',
url: "registro1.php",
data: "costo="+cost+"&producto="+product+"&precio="+preci+"&stock="+stoc,
success: function(resp){
$('#respuesta').html(resp);
Limpiar1();
Cargar1();
}
});
}
function Modificar1(){
var cost = $("#costo").val();
var product = $("#producto").val();
var preci = $("#precio").val();
var stoc = $("#stock").val();
$.ajax({
type: "POST",
dataType: 'html',
url: "registro1.php",
data: "costo="+cost+"&producto="+product+"&precio="+preci+"&stock="+stoc,
success: function(resp){
$('#respuesta').html(resp);
Limpiar1();
Cargar1();
}
});
}
function buscar(){$(document).ready(function() {
$.ajax({
url: 'registro1.php',
type: 'GET',
dataType: 'json',
data: { id : $('#id').val()}
}).done(function(respuesta){
$('#costo').val(respuesta.costo);
$('#producto').val(respuesta.producto);
$('#precio').val(respuesta.precio);
$('#stock').val(respuesta.stock);
})})}
---------------------ARCHIVO index1.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
<!DOCTYPE html>
<html lang="es">
<head>
<title>Smartjob</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style1.css" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="ajax.js"></script>
<script type= "text/javascript" src= "http://code.jquery.com/jquery.min.js"></script>
<script src="main.js"></script>
<script src="jquery.min.js"></script>
</head>
<body>
<header>
<nav>
<h2>Productos en Stock</h2>
</nav>
<header>
<div id="contenedor1">
<div id="inscribir1">
<section class="widget11">
<h4 class="widget11titulo">Nuevo producto</h4>
<form name="agenda" method="post" action="registro1.php" >
<input type="text" name="id"placeholder="id" tabindex= "0" size="20" autocomplete= "off">
<input type="text" id= "costo" name="costo"placeholder="costo" autocomplete= "off" size="20" >
<input type="text" id= "producto" name="producto"placeholder="producto" autocomplete= "off" size="20">
<input type="text" id="precio" name="precio"placeholder="precio" autocomplete= "off" size="20">
<input type="text"id="stock" name="stock"placeholder="stock" autocomplete= "off" size="20">
<label><input type="radio" id="tipo" name="tipo" value=I checked>Ingreso</label>
<label><input type="radio" id="tipo" name="tipo" value=E>Egreso</label>
<br>
<button onclick="Registrar1()" id="registrar1" name= "registrar1"tabindex="7"value="registrar1">Guardar
<button onclick="Modificar1()" id="modificar1"name="modificar1"tabindex="8"value="modificar1">Modificar
<button onclick="Eliminar1()" id="eliminar1" name="eliminar1" tabindex="9"value="eliminar1">Eliminar
<button onclick="buscar()" id="buscar" name="buscar" tabindex="10"value="buscar">Buscar
<br style="clear:both;">
</form>
<div id="respuesta"></div>
</section>
</div>
<div id="consultar1">
<section class="widget12">
<h4 class="widget12titulo">Productos</h4>
<div class="datagrid1" id="datagrid1" >
</div>
</section>
</div>
</div>
</body>
</html>
----------------------ARCHIVO registro1.php (en este están todos los pedidos con el servidor, el conexion.php lo tengo aparte)
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
<?php
if (isset($_POST['registrar1'])) {
require("conexion.php");
$cost = $_POST['costo'];
$product = $_POST['producto'];
$preci = $_POST['precio'];
$stoc = $_POST['stock'];
$consulta = "INSERT INTO stock (costo,producto,precio,stock) VALUES ('$cost','$product','$preci','$stoc')";
mysqli_query($con, $consulta);
if($consulta)
{ echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=index1.php'>";
}
else
{
echo "No se pudieron guardar los datos";
}
}
if (isset($_POST['eliminar1'])) {
require("conexion.php");
$i = $_POST['id'];
$consulta = mysqli_query($con, "DELETE FROM stock where id = '$i' ");
mysqli_query($con, $consulta);
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=index1.php'>";
}
if (isset($_POST['modificar1'])) {
require("conexion.php");
$i = $_POST['id'];
$cost = $_POST['costo'];
$product = $_POST['producto'];
$preci = $_POST['precio'];
$stoc = $_POST['stock'];
$consulta2 = "UPDATE stock SET costo='$cost',producto='$product',precio='$preci',stock='$stoc' where id='$i'";
mysqli_query($con, $consulta2);
echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=index1.php'>";
}
if (isset($_POST['buscar'])) {
include("conexion.php");
$id=$_POST['id'];
$query ="SELECT * FROM stock where id='$id'";
$resultado = $con->query($query);
$datos = new stdClass();
if($resultado->num_rows > 0){
$fila = $resultado->fetch_array();
$datos->costo= $fila['costo'];
$datos->producto= $fila['producto'];
$datos->precio= $fila['precio'];
$datos->stock= $fila['stock'];
}
echo json_encode($datos);
//echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=index1.php'>";
$con->close();
}
?>
Muchas gracias!
Valora esta pregunta


0