0% found this document useful (0 votes)
32 views9 pages

Formulario 1

The document contains code for two HTML forms with inputs for name, quantity, and price that submit to PHP files to insert the data into a database table. The second form uses AJAX to submit the form and display the response.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views9 pages

Formulario 1

The document contains code for two HTML forms with inputs for name, quantity, and price that submit to PHP files to insert the data into a database table. The second form uses AJAX to submit the form and display the response.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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">

<button class="btn btn-outline-black btn-rounded "


type="submit">Enviar</button>
</form>
</div>

</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";

$mysqli = new mysqli ($host,$user,$pwd,$db);

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

<?php

require_once('../lib/conexion.php');

$nombre = $_POST['nombre'];
$cant = $_POST['cantidad'];
$precio = $_POST['precio'];

$sent = "INSERT INTO


producto(nom_prod,cant_prod,precio_prod) VALUES
('$nombre','$cant','$precio')";

echo 'por '.$cant.' '.$nombre.' tiene que pagar : '.($precio*$cant);

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">

<button id="enviar" class="btn btn-outline-


black btn-rounded " type="submit">Enviar</button>
</form>
<div class="respuesta"></div>
</div>
</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>
<script>

$(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";

$mysqli = new mysqli ($host,$user,$pwd,$db);

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";
}
?>

You might also like