Alert when sending message

0

It happens that I am making a small contact form for my site, I have almost finished everything, however I want that when you press the send button instead of sending me to another page, just show an alert saying that the message already sent.

<?php

    $destino = "******************";
    $nombre = $_POST["Nombre"];
    $telefono = $_POST["Telefono"];
    $correo = $_POSTO["Correo"];
    $mensaje = $_POST["Mensaje"];

    $contenido = "Nombre: ".$nombre. "\nTelefono: " .$telefono. "\nCorreo: " .$correo. "\nMensaje: " .$mensaje;
    mail($destino, "Cotizacion", $contenido);
    header("Location: index.html");

?>

This is my current php code.

    
asked by Hugo Costilla 08.05.2018 в 11:55
source

2 answers

1

In this case the line header("Location: index.html"); effectively redirects you to another page. If you want to stay on the same page you must remove that line.

Also, you can check if there is no error in sending the message, something like this:

<?php

    $destino = "******************";
    $nombre = $_POST["Nombre"];
    $telefono = $_POST["Telefono"];
    $correo = $_POSTO["Correo"];
    $mensaje = $_POST["Mensaje"];

    $contenido = "Nombre: ".$nombre. "\nTelefono: " .$telefono. "\nCorreo: " .$correo. "\nMensaje: " .$mensaje;
    $resultado=mail($destino, "Cotizacion", $contenido);

    if($resultado){
        echo "Mensaje enviado con éxito";
    }else{
        echo "Hubo un error enviando el mensaje";
    }
    //header("Location: index.html");

?>
    
answered by 08.05.2018 в 12:02
0
<?php

$destino = "******************";
$nombre = $_POST["Nombre"];
$telefono = $_POST["Telefono"];
$correo = $_POSTO["Correo"];
$mensaje = $_POST["Mensaje"];

$contenido = "Nombre: ".$nombre. "\nTelefono: " .$telefono. "\nCorreo: " .$correo. "\nMensaje: " .$mensaje;
$resultado = mail($destino, "Cotizacion", $contenido);

if($resultado == true){
  echo '<script language="javascript">alert("Su mensaje ha sido enviado")<script>';
}else {
  echo echo '<script language="javascript">alert("Error al mandar mensaje")<script>';
}

 ?>
    
answered by 08.05.2018 в 12:12