Send an Ajax form without updating the page, just open a modal on the screen

Asked

Viewed 42 times

0

Hello, I have a form that uses Phpmailer to send the data, but whenever I send it changes the page going to a white screen, I wanted instead to go to this white screen only open a modal on the screen that has the form. Ai wondered, how do I do this using Ajax request?

Formúlario:

<form class="d-md-flex flex-column" action="envia_fale.php" method="post" name="form">
    <div class="form-floating mb-3">
        <input type="text" class="form-control" id="floatingNome" required name="nome" placeholder="Seu nome">
        <label for="floatingNome" class="text-size-16-regular ps-5">Seu nome</label>
    </div>
    <div class="d-md-flex">
        <div class="form-floating mb-3 input-email pe-lg-2">
            <input type="email" class="form-control" id="floatingInput" required name="email" placeholder="Seu Email">
            <label for="floatingInput" class="text-size-16-regular ps-5">Seu Email</label>
        </div>
        <div class="form-floating mb-3 input-telefone ps-lg-2">
            <input type="tel" class="form-control" id="floatingTelefone" required name="telefone"
                placeholder="Seu telefone">
            <label for="floatingTelefone" class="text-size-16-regular ps-5">Seu telefone</label>
        </div>
    </div>
    <div class="form-floating mb-3">
        <input type="text" class="form-control" id="floatingEmpresa" required name="empresa" placeholder="Empresa">
        <label for="floatingEmpresa" class="text-size-16-regular ps-5">Empresa</label>
    </div>
    <input type="submit" class="text-size-20 btn btn-contato align-self-end" value="Baixe a Apresentação" />
</form>

Here is the code of the upload_fale.php file phpmailer:

<?php

define('__ROOT__', dirname(dirname(__FILE__)));
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require __ROOT__ . '/telemetria/PHPMailer/src/Exception.php';
require __ROOT__ . '/telemetria/PHPMailer/src/PHPMailer.php';
require __ROOT__ . '/telemetria/PHPMailer/src/SMTP.php';

$nome = utf8_encode($_POST['nome']);
$email = utf8_encode($_POST['email']);
$telefone = utf8_encode($_POST['telefone']);
$empresa = utf8_encode($_POST['empresa']);

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();

    // Configurações do servidor de email
    $mail->Host = 'xxx.xxx.com.br';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'xxxxxx';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = xxx;

    $mail->setFrom($mail->Username, "xx");
    $mail->addAddress('[email protected]');
    $mail->Subject = 'xx xx';

    $conteudo_email = "
        Email da xx:<br><br>

        Nome:$nome<br>
        Email:$email<br>
        Telefone:$telefone<br>
        Empresa:$empresa<br>
    ";

    $mail->IsHTML(true);
    $mail->Body = $conteudo_email;
    $mail->send();
    echo 'index.php'; 
} catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>
No answers

Browser other questions tagged

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