Send Javascript confirmation message

Asked

Viewed 4,072 times

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, I will study this form with ajax, thanks for the suggestion.

1 answer

3


You are mixing things. PHP runs on the server while JS runs on the client (browser). Writing JS via PHP is possible but not a good practice.

To resolve this issue, remove the header and include window.location.href, being like this:

echo '
    <script>
        alert("Mensagem enviada com sucesso!");
        window.location.href = "index.html";
    </script>
';

Still, as @Oeslei commented, I suggest redirecting to another page that displays the message or send by AJAX.

  • This way, it doesn’t redirect me to i index.html, it just shows that it is inside the ('') page sendemail.php.

  • I will study this form with ajax, thanks for the suggestion.

  • Try removing the die or the header('Content-type: application/json');, pq here is working

  • Really, it was the header that was blocking. Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.