Successful Toast dialog when sending the form with PHP

Asked

Viewed 896 times

1

I am creating a form for my personal website, where the user will fill in with name, surname, email, password and message and this information will be sent to my personal email.

Sending the form is already working perfectly, with the PHP Lib Phpmailer.

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {

    echo "Enviado com sucesso!"
}

However I wish instead of echo "Enviado com sucesso!", a message of success or failure was displayed with the Toast Dialogue of the Materialize Framework.

1 answer

1


It’s very simple, do it this way:

if (!$mail->send()) {
    echo "<script>
            $(function(){
                Materialize.toast('Mailer Error: ".$mail->ErrorInfo."' , 2200)
            }
            });  
    </script>";
} else {
    echo "<script>
            $(function(){
                Materialize.toast('Enviado com sucesso!' , 2200)
            }
            });  
    </script>";
}

Or

send-msg.php

if (!$mail->send()) {
    header("Location: pag_onde_vc_mostra_a_msg.php?acao=fail");
} else {
    header("Location: pag_onde_vc_mostra_a_msg.php?acao=ok");
}

pag_onde_vc_mostra_a_msg.php

if(isset($_GET['acao'])){
    $msg = "";
    if($_GET['acao'] == 'ok'){
        $msg = 'Mailer Error: ".$mail->ErrorInfo."';
    }
    if($_GET['acao'] == 'fail'){
        $msg = 'Enviado com sucesso!';
    }
    echo "  <script>
                $(function(){
                    Materialize.toast('".$msg."' , 2200)
                });  
            </script>";
}
  • Hi Wictor your script really seems to me to be the correct way, however when I click the SEND button I get sent to the file envia-msg.php (responsible for the logic to send email). But what happens is that I would like to stay on the same page, only with the messages of Toast.

  • Not your send-msg page you will have to use the header command("Location: pagina_onde_voce_esta?acao=ok.php") and pass via get ok or fail and on your main page check if there is if(isset($_GET['action'])) and check whether it was ok or not, so you put the messages on the main page.

  • @Matheusnascimento I made a change according to your comment.

  • 1

    Perfect... Thank you!

Browser other questions tagged

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