I want to give an answer to the user, either: "Sent" or "Could not send" on the same page you see, after sending an email using a form ... Let me explain:
1: The user enters > Contact.html in the browser, fill in the form and click on the send button, so that:
I have attached the html5 code:
<form id="formulario" action="php/sendEmail.php" method="POST">
<fieldset class="txtCentral txtDerecha">
<input type="text" id="nombre" class="fade inForm" name="nombre_txt" placeholder="Nombre *" required/>
<input type="text" id="apellido" class="fade inForm" name="apellido_txt" placeholder="Apellido"/>
<input type="email" id="email" class="fade inForm" name="email_txt" placeholder="Email *" required/>
<input type="text" id="asunto" class="fade inForm" name="asunto_txt" placeholder="Asunto"/>
<textarea id="mensaje" class="fade inForm" name="mensaje_txt" cols="31" rows="6" placeholder="Comentarios *" required></textarea>
<p class="txtRotulo">* Campos obligatorios.</p>
<div class="botonCentro">
<input type="submit" id="enviar" class="fade botonLink" name="enviar_btn" value="Enviar"/>
</div>
</fieldset>
</form>
<div class="txtCentral" id="respuestaCorreo"></div>
2: The user expects the operation to send the mail, once the operation is done ... Well, as you can see, my intention is to put text in that "div" that has id="mailresponse" (The last line of the html5 code).
But add that text ... that breaks my head ... since I have to do it from php / sendEmail.php (location / NombreFichPHP), come on, from the php code.
The code in php is such that:
<?php
/* Variables de fichero PHP. */
$destinatario = '[email protected]';
/* variables de formulario necesarias. */
$nombre = $_POST['nombre_txt'];
$email = $_POST['email_txt'];
$mensaje = $_POST['mensaje_txt'];
/* Variables de formulario opcionales. */
$apellido = $_POST['apellido_txt'];
$asunto = $_POST['asunto_txt'];
/* Comprobamos valores. */
if(isset($nombre) && !empty($nombre) &&
isset($email) && !empty($email) &&
isset($mensaje) && !empty($mensaje)){
/* Dar valores a las variables auxiliares si estan vacías. */
if(isset($apellido) && empty($apellido)){
$apellido = "";
}else{
/* Lo concatamos con el nombre. */
$nombre .= " ".$apellido;
}
if(isset($asunto) && empty($asunto)){
$asunto = "Sin asunto";
}
/* Generamos contenido del correo*/
$contenido = "
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
</head>
<body>
<header>
<h1>Correo</h1>
</header>
<div>
<P>Usuario: ".$nombre."</p>
<p>Correo: ".$email."</p>
<p>Mensaje: ".$mensaje."</p>
</div>
</body>
</html>
";
/* Generamos la cabecera del correo. */
$cabecera = "MIME-Version: 1.0\r\n";
$cabecera .= "Content-type: text/html; charset = UTF-8\r\n";
$cabecera .= "From: ".$email."\r\n";
/* Enviamos el correo guardando su estado. */
$enviado = mail($destinatario, $asunto, $contenido, $cabecera);
}
/* Mostramos información al usuario y redireccionamos. */
if($enviado){
echo '<script language="javascript">
window.onload = function() {
alert("enviado");
}
</script>';
}else{
echo '<script language="javascript">
window.onload = function() {
alert("noEnviado");
}
</script>';
}
?>
3: The user sees the answer below the form, which is where the previously mentioned div is. If the email was sent correctly, the form is restarted, if not, then the data is left, so that it corrects them.
As you can see, what I have tried is to show a pop using javaScript ... but I do not think the truth is nice ... so I ask ... I suppose it could be, but I do not know how ... I do not know much about php.
Surely there is some function or something similar that is not just "echo".
Programmers of PHP I acclaim you! xD (Thank you very much from Ante Mano).