Query to show data of a form

1

How do I make my computer display the data on the same page of a form simultaneously when they are sent to a database? It is for a comments section that I have planned to put on the web page that I am creating and also put the date and time respective to each comment

This is the form:

<form action="insertar.php" method="POST">
        <Label for="nombre">Nombre:</Label>
        <input type="text" name="nombre" required=""placeholder="Escribe tu nombre..."><br><br>
        <label for="email">Email:</label>
        <input type="email" name="email"  required=""placeholder="Escribe tu email..."> <br><br>
        <label for="clave">Clave:</label>
        <input type="password" name="clave" required="" placeholder="Escribe tu clave..."><br><br>
        <label for="comentario">Comentario:</label>
       <textarea type="comentario" name="comentario" cols="90px" rows="8" required="" placeholder="Escriba aquí su comentario..."></textarea><br><br>
        <input type="submit" value="Comentar">

    </form>

This is the PHP connection:

<?php 

$PDO = new PDO('mysql:host=localhost;dbname=db_comentarios;charset=UTF8','root','' );
try { 
  echo "datos enviados correctamente";
$sql=$PDO->prepare("INSERT INTO prueba1(nombre, email, clave, comentario) VALUES (:nombre, :email, :clave, :comentario)");
$sql->bindParam(':nombre',$_POST['nombre']);
$sql->bindParam(':email',$_POST['email']);
$sql->bindParam(':clave', password_hash($_POST['clave'], PASSWORD_DEFAULT));
$sql->bindParam(':comentario',$_POST['comentario']);
$sql->execute();
 }catch(PDOException $e) {
        echo "Fallo de conexion al enviar los datos:".$e->getMessage();
 }

 ?>
    
asked by Mateo Gomez 27.12.2018 в 00:21
source

1 answer

0

As I read your approach to the problem, you should reload the page, after making the insertion of your data, obviously you should also show the comments you have in your DB in divs or in a table
Quick solution:


//Tu conexion
$PDO = new PDO('mysql:host=localhost;dbname=db_comentarios;charset=UTF8','root','' );
try {
  //La insercion de la información
  $sql=$PDO->prepare("INSERT INTO prueba1(nombre, email, clave, comentario) VALUES (:nombre, :email, :clave, :comentario)");
  $sql->bindParam(':nombre',$_POST['nombre']);
  $sql->bindParam(':email',$_POST['email']);
  $sql->bindParam(':clave', password_hash($_POST['clave'], PASSWORD_DEFAULT));
  $sql->bindParam(':comentario',$_POST['comentario']);
  $sql->execute();
//Verificas si la información se agrego exitosamente
  if($sql){
      //Le muestras al usuario un mensaje de feedback y lo rediriges al sitio que tu quieras
      echo "alert('datos enviados correctamente'); location.href = 'http://www.example.com/'; ";
}else{ // este es el caso contrario, solo cambia el mensaje echo "alert('error al enviar los datos'); location.href = 'http://www.example.com/'; "; } }catch(PDOException $e) { echo "Fallo de conexion al enviar los datos:".$e->getMessage(); }

Also the implementation of AJAX is more useful as I commented shadow

    
answered by 27.12.2018 в 00:39