0
I have a method to send Email via PHP that is working correctly.
I would just like you to at the end send a reply to the user, by a alert
javascript same.
My form is like this:
<form class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Nome *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Telefone</label>
<input type="text" class="form-control" name="telefone">
</div>
<div class="form-group">
<label>Empresa</label>
<input type="text" class="form-control" name="empresa">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Assunto *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Mensagem *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Enviar Mensagem</button>
</div>
</form>
And this is my method of sending the Email.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email enviado!'
);
//pega os atributos do form
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$telefone = @trim(stripslashes($_POST['telefone']));
$empresa = @trim(stripslashes($_POST['empresa']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = '[email protected]';//email remetente
//elabora o texto
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Telefone: ' . $telefone . "\n\n" . 'Empresa: ' . $empresa . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
//envia o email
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
//verifica se foi enviado e redireciona para a página
if(success){
//tentei adicionar esse alert, porem sem sucesso
echo "<script>alert(\"Mensagem enviada com sucesso!\")</script>";
//redireciona para o index
header("Location: index.html");
}
die;
As stated in the script comment, I tried to add the message before redirecting to my index, but without success.
By submitting the form, you are already redirecting (to
sendemail.php
). An alternative would be to redirect to a page that displays a success message. Another alternative, more robust, is to use ajax to send the email. Search for ajax on the internet, content not lacking...– Oeslei
@Oeslei, I will study this form with ajax, thanks for the suggestion.
– Randrade