Formulario 1
Formulario 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>formulario</title>
<meta name="viewport" content="width=device-width, initial-
scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/font-awesome.css">
<link rel="stylesheet" href="css/all.css">
<link rel="stylesheet" href="css/mdb.css">
</head>
<body>
<br>
<br>
<br>
<br>
<div class="row">
<div class="col-3"></div>
<div class="col-6">
<form action="prueba/datos.php"method="post"class="
border border-light pt-5">
<label for="">Nombre</label>
<input type="text" name="nombre" class="form-
control">
<label for="">Cantidad</label>
<input type="text" name="cantidad" class="form-
control">
<label for="">Precio</label>
<input type="text" name="precio" class="form-
control">
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/mdb.min.js"></script>
</body>
</html>
CONEXION
<?php
$host="localhost";
$user="root";
$pwd="";
$db="redsocial";
if ( $mysqli ->connect_errno) {
echo "Falló la conexión a la Base de Datos";
}
return $mysqli;
?>
DATOS
<?php
require_once('../lib/conexion.php');
$nombre = $_POST['nombre'];
$cant = $_POST['cantidad'];
$precio = $_POST['precio'];
header("Refresh:5; url=../formulario.php");
?>
FORMULARIO 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>formulario</title>
<meta name="viewport" content="width=device-
width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/font-awesome.css">
<link rel="stylesheet" href="css/all.css">
<link rel="stylesheet" href="css/mdb.css">
</head>
<body>
<br>
<br>
<br>
<br>
<div class="row">
<div class="col-3"></div>
<div class="col-6">
<form id="formulario2" action=""method="post"class=" border
border-light pt-5">
<label for="">Nombre</label>
<input type="text" name="nombre"
class="form-control">
<label for="">Cantidad</label>
<input type="text" name="cantidad"
class="form-control">
<label for="">Precio</label>
<input type="text" name="precio"
class="form-control">
$(document).ready(function(){
$('#enviar').on("click", function(e){
e.preventDefault();//evita que la pagina se
actualise
var frm = $('#formulario2').serialize();//conpactar
todos los datos a una sola variable
$.ajax({
type:'POST',
url:'prueba/datos.php',
data: frm,
success:function(resp){
$('.respuesta').html(resp);
}
});
});
});
</script>
</body>
</html>
CONEXIÓN 2
<?php
$host="localhost";
$user="root";
$pwd="";
$db="productos";
if ( $mysqli ->connect_errno) {
echo "Falló la conexión a la Base de Datos";
}
mysqli_set_charset($mysqli,"utf8mb4"); //para que la
conexión entregue los acentos
return $mysqli;
?>
DATOS 2
<?php
require_once('../lib/conexion2.php');
$nombre = $_POST['nombre'];
$cant = $_POST['cantidad'];
$precio = $_POST['precio'];
$sent = "INSERT INTO
producto(nom_prod,cant_prod,precio_prod) VALUES
('$nombre','$cant','$precio')";
$resp = $mysqli->query($sent);
if ($resp){
echo "los datos se guardaron correctamente";
}else{
echo "no se guardaron los datos";
}
?>