Send data to BBDD from a Form with PHP without reloading the Web


<form action="db_principal.php" method="post" enctype="multipart/form-data">

<textarea placeholder="En que estas pensando?" name="detalle" required=""></textarea>

<input id="coords" type="text" class="form-control" name="coords" value="input 1" data-toggle="modal" data-target="#modalGmap" required=""/>

<input type="text" class="form-control" name="titulo" placeholder="input 2" required=""/>

<button id="enviar" type="submit" class="btn btn-success green" name="publicar"></button>


And this PHP test file


$servername = "localhost";
$database = "coordenadas";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());

echo "Connected successfully";

$coords = $_POST['coords'];
$detalle = $_POST['detalle'];
$titulo = $_POST['titulo'];

$sql = "INSERT INTO lugares (coords, detalle, titulo) VALUES ('$coords', '$detalle', '$titulo')";
if (mysqli_query($conn, $sql)) {
        header("Location: index.html");

} else {
      echo ", Error: " . $sql . "<br>" . mysqli_error($conn);

What happens is that while everything works perfectly ... When I click the "publish" button the data is sent to the database but it takes me to the following link localhost/root/db_principal.php . If you see the code of PHP you will notice a part that says header("Location: index.html"); This does redirect the user to the web index.html ... But it does not help either. I need that when clicking on "Publish" action="db_principal.php" of form is executed without reloading the web. It is worth considering that this happens through the method POST

asked by Macali 21.12.2018 в 19:31

0 answers