View confirmation modal after sending email

Asked

Viewed 1,214 times

1

I have the following code

<?php

require_once('PHPMailer-master/PHPMailerAutoload.php');

PHP_OS == "Linux" ? $quebra_linha = "\n" : $quebra_linha = "\r\n";

//$emp = $_POST["empresa"];
$nome = trim($_POST["nome"]);
$tel = $_POST["telefone"];
$cid = $_POST["cidade"];
//$ass = $_POST["assunto"];
//$msg = $_POST["mensagem"];

switch ($cid) {
    case "ipatinga":
        $destino = "[email protected]";
        break;
    case "fabriciano":
        $destino = "[email protected]";
        break;
    case "timoteo":
        $destino = "[email protected]";
        break;
}


$email = "<br><b>NOME:</b> ".$nome."<br><b>TELEFONE:</b> ".$tel." <br><b>CIDADE:</b> ".$cid."</body></html>";

$mail = new PHPMailer();

$mail->IsMail();
//$mail->SetFrom('[email protected]', 'Thiago Londe');
$mail->SetFrom($eml, $nom);
$mail->Subject    = "Agendamento de visita";
$mail->MsgHTML($email);
//$mail->AddAddress($destino, "Thiago Londe");
$mail->AddAddress($destino, utf8_decode('Unimed'));


if (!$mail->send()) {
    $msgerro = "Não foi possível enviar sua mensagem de contato. Tente novamente dentro de alguns instantes.";
} else {
    $msgerro = "Sua mensagem de contato foi enviada com sucesso!";
}

if($mail->send()) { ?>

<script language='javascript'>
    alert('<?php echo $msgerro; ?>');
    window.open('../#agendar-visita','_self');

</script>

I would like to know some way to display a submission confirmation modal as soon as the user submits the form.

Any suggestions?

1 answer

2

In my view, the simplest way to build any front-end, including modals, is by using boostrap. In this case, you could do as follows:

<?php

function error($data) {
    foreach($data as $key => $val)
        if(empty($val))
            return "Campo $key vazio, preencha todos os campos para fazer o envio";
    return false;
}

require_once('PHPMailer-master/PHPMailerAutoload.php');

PHP_OS == "Linux" ? $quebra_linha = "\n" : $quebra_linha = "\r\n";

//$data["empresa"] = $_POST["empresa"];
$data["nome"] = trim($_POST["nome"]);
$data["telefone"] = $_POST["telefone"];
$data["cidade"] = $_POST["cidade"];
//$data["assunto"] = $_POST["assunto"];
//$data["mensagem"] = $_POST["mensagem"];
try {
    $error = error($data);
    if($error)
        throw new Exception($error);

    switch ($cid) {
        case "ipatinga":
            $destino = "[email protected]";
            break;
        case "fabriciano":
            $destino = "[email protected]";
            break;
        case "timoteo":
            $destino = "[email protected]";
            break;
    }



    $email = "<br><b>NOME:</b> ".$nome."<br><b>TELEFONE:</b> ".$tel." <br>        <b>CIDADE:</b> ".$cid."</body></html>";

    $mail = new PHPMailer();

    $mail->IsMail();
    //$mail->SetFrom('[email protected]', 'Thiago Londe');
    $mail->SetFrom($eml, $nom);
    $mail->Subject    = "Agendamento de visita";
    $mail->MsgHTML($email);
    //$mail->AddAddress($destino, "Thiago Londe");
    $mail->AddAddress($destino, utf8_decode('Unimed'));


    if (!$mail->send())
         throw new Exception("Não foi possível enviar sua mensagem de contato. Tente novamente dentro de alguns instantes.");

    $msgerro = "Sua mensagem de contato foi enviada com sucesso!";
} catch (Exception $e) {
    $msgerro = $e->getMessage(); 
}

?> 
<!DOCTYPE html>
<html lang="en">
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
    <div class="modal fade" id="modal-mail" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Envio de email</h4>
          </div>
          <div class="modal-body">
            <p><?=$msgerro?></p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div>
    <script>
        $(function(){
            $('#modal-mail').modal('show');
        });
    </script>

</body>
</html>
  • Thanks Kenny Rafael. But how can I do this inside php? I have this: if (!$mail->send()) { $msgerro = "Could not send your contact message. Try again in a few moments." ; } Else { $msgerro = "Your contact message has been sent successfully!" ; } .

  • I’ll edit it to make it clearer!

  • You are already setting the message, regardless of error or not, in this case, just give echo inside the modal as in the example, what I changed was actually just enter your upload code before displaying the content.

  • I got Kenny Rafael. Now I have another question. My form is validating to see if the fields have all been filled out. My modal is appearing even if the fields are not filled in. How to make it appear only if all fields are filled in? I left it like this: $('form button[type="Submit"]'). on('click', Function() { $('#modal-mail'). modal('show'); });

  • The ideal is that your modal always open with the information of what happened, in this case I upgraded the code, take a look!

Browser other questions tagged

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