How do I get back to the form page when sending a message?

Asked

Viewed 2,310 times

1

After sending the message Alert appears, but the browser does not return pro contact.html (form).

Then I wonder if I can create a style for this alert too.

send php.

// ENVIO DO EMAIL
$enviado = $mail->Send();
// Limpa os destinatários e os anexos
$mail->ClearAllRecipients();

// verifica se enviou corretamente
if ( $enviado ) {
    echo "<script>
        alert('Pedido enviado!');
    </script>";
}
else {
    echo "Não foi possível enviar o e-mail.";
    echo "<b>Detalhes do erro:</b> " . $mail->ErrorInfo;
}
header('Refresh: 10; Location: contact.html');
  • place the contents of contact.html

2 answers

2


You have two alternatives, create an IF and ELSE to go back to the page you want, or create Javascript code for the page or redirect as the user gives Ubmit.

1st alternative

// ENVIO DO EMAIL
$enviado = $mail->Send();

// Limpa os destinatários e os anexos
$mail->ClearAllRecipients();



// verifica se enviou corretamente
if ($enviado) {
    echo "<script>alert('Pedido enviado!');location.href='contact.html';</script>";
}
else {
    echo "Não foi possível enviar o e-mail.";
    echo "<b>Detalhes do erro:</b> " . $mail->ErrorInfo;
    echo "<script>location.href='contact.html';
}

alternative if data are sent via a type="submit"

  $(document).ready(function(){
    $('.ajax').submit(function(){

            var dados = $(this).serialize();


            $.ajax({
                    type: "POST",
                    url: "enviar.php",
                    data: dados,
                    success: function( data )
                    {
                            alert( data );
                    }
            });

            return false;
    });
});

but in your form you’ll have to add a class to work and it would be kind of like this

<form action=''method='' class='ajax'>
  • Did @Alanvieira

  • Great response @Alanvieira

  • thanks @avsinacio the answer is mine

  • Ops... Great solution @Victor

1

Just set up a Location header, the message will be displayed in javascript, after the click (either cancel or OK), it returns to contact.html, as desired.

// ENVIO DO EMAIL
$enviado = $mail->Send();
// Limpa os destinatários e os anexos
$mail->ClearAllRecipients();



// verifica se enviou corretamente
    if ( $enviado ) {

        echo "<script>
            if(confirm("Ok, enviado com sucesso")){
                  <?php header("Location: contact.html"); ?>
            }
        </script>";

    }
    else {
        echo "Não foi possível enviar o e-mail.";
        echo "<b>Detalhes do erro:</b> " . $mail->ErrorInfo;
    }
  • I did it this way, but the message is sent and the alert does not appear.

  • @Alanvieira try as I indicated in my reply, here it worked the 1st alternative met your case ?

  • I don’t understand why I should deny the answer...

  • was unintentionally intended to be positive, edit her for me to withdraw the negative and positive

  • You can do it this way, I edited the question :)

  • ready @Andrébaill sends a positive for me too rsrs

  • @Victor the first solution was good, I found interesting also :D

  • 1

    thanks, its also, is programming demonstrating once again that several logics generate same result and performance

Show 3 more comments

Browser other questions tagged

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