Send confirmation message JavaScript

0

I have a method to send Email via PHP that is working correctly.

I would just like you to send an answer to the user, for a alert of the same javascript.

My form looks 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 for sending mail.

<?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.

    
asked by anonymous 31.03.2015 / 21:48

1 answer

3

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

To solve this problem, remove the header of PHP and include window.location.href , thus:

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 sending it via AJAX.

    
31.03.2015 / 22:12