Confirmation message phpmailer

0

I'm doing a web (I'm just learning) and I made a contact form with phpmailer, it sends me everything correctly but what I want is to change the confirmation message when the email is sent, so now it redirects me to contact.php and I shows jn common text what I would like is to show me an alert or to see the message that was sent under my form if possible without recharge, investigate and probe but nothing works for me. Thanks in advance my index.html is like this

<form name="sentMessage" method="post" action="php/contacto.php" id="contact-form" role="form" class="animated bounceInLeft">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Nombre *" name="name" required="" data-validation-required-message="Por favor ingrese su nombre.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="email" class="form-control" placeholder="E-mail *" name="email" required="" data-validation-required-message="Por favor un e-mail valido.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="tel" class="form-control" placeholder="Numero *" name="phone" required="" data-validation-required-message="Por favor ingrese su numero .">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <textarea class="form-control" placeholder="Mensaje *" name="message" required="" data-validation-required-message="Porfavor ponga un mensaje."></textarea>
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div id="success"></div>
            <button type="submit" id="submit" form-id="contactForm" class="btn btn-primary btn-lg get enviar-boton">Enviar</button>
        </div>
    </div>
</form>

And my php using phpmailer ends in:

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo '<script type="text/javascript">alert("Gracias por contactarnos");</script>';
}

I only get the alert on a new page and then blank page.

    
asked by Jean Carlos Ventura 28.08.2017 в 03:23
source

2 answers

1

As you have this mounted, after sending the mail you should redirect with a header () to the same contact page and pass a parameter to know that everything has gone right, and change your contact.php so that after the form, read if that variable exists, and if it exists, paint the message you want, for example:

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
   header("location: contact.php?enviado=1");
}

The php of sending the email would be like that, on the other hand .... in your contact.php you should add this:

<!-- El resto de tu código vendria aqui arriba, por no copiar todo -->
<div class="col-lg-12 text-center">
        <div id="success"></div>
        <button type="submit" id="submit" form-id="contactForm" class="btn btn-primary btn-lg get enviar-boton">Enviar</button>
    </div>
<?php 
     if(isset($_GET['enviado']){
         if($_GET['enviado'] == 1){
             echo '<script type="text/javascript">alert("Gracias por contactarnos");</script>';
         }
     }
?>

This way, when someone enters your page, no message will appear, but when filling out the form and redirecting to the page with that parameter "sent = 1", it already detects it and shows you the message.

But I'm telling you, this kind of things are not done that way, you should use javascript, jQuery to make it easier for you and make all these requests by AJAX to send a data php and return a response without recharging the page.

I hope you understood, a greeting.

    
answered by 28.08.2017 в 13:49
0

They know I was trying everything with ajax but still they keep sending me to my contact php after sending the form

    
answered by 30.08.2017 в 20:59