Form redirecting

Asked

Viewed 54 times

1

Hello!

So, I created a form in PHP and JS, but it redirects to a blank page only saying that it was sent. The problem is that it should appear on the same page without being redirected.

This is the HTML

<section id="formFolder">
            <img class="imgWire" src="assets/img/main/Wire-PNG.png" alt="">
            <img class="imgWire2" src="assets/img/main/Wire-PNG.png" alt="">
            <div class="container">
                <form id="contact-form-folder" method="post" action="mail/contact-folder.php" role="form">
                    <div class="row">
                        <div class="col-md-12 divFormBlog">
                            <div class="col-md-6">
                                <h4 class="text-center folderText">FOLDER DIGITAL</h4>
                                <h5 class="text-center recebaText">Receba nosso folder no seu e-mail:</h5>
                            </div>
                            <div class="col-md-2 inputDiv1">
                                <input type="text" name="name" placeholder="Digite seu nome">
                            </div>
                            <div class="col-md-2 inputDiv2">
                                <input type="text" name="email" placeholder="Digite seu e-mail">
                            </div>
                            <div class="col-md-2 divLink">
                                <input type="submit" class="btn btn-success btn-send btnEnviarForm btnEnviar" value="Enviar">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </section>

This is the php:

<?php


// contact-folder.PHP - CONTACT FORM SEND FILE - THINKBEYOND [ DEV TEAM ]
// www.tbdesenvolvimento.com.br - #OFuturoÉOnline


// configure
$from = 'Formulario de Contato <[email protected]>';
$sendTo = 'Contato <[email protected]>';
// $sendTo = 'Contato <[email protected]>';
$subject = 'Folder Digital - Nova solicitação';
$fields = array('nome' => 'Nome ',  'email' => 'Email ', 'phone' => 'Telefone '); // array variable name => Text to appear in the email
$okMessage = 'Folder digital enviado!' ;
$errorMessage = 'Ops, ocorreu um erro, por favor tente novamente.';

// let's do the sending
try
{
    $emailText = "Olá JF Security, você recebeu uma solicitação do seu folder digital através do formulário do seu site.\n\nLembre-se de responder a essa mensagem através do email fornecido abaixo pelo visitante do seu site!\n\n\nAqui estão as informações:\n\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }

    }

require("class.phpmailer.php");
require("class.smtp.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From      = '[email protected]';
$mail->FromName  = 'Formulário de Contato';
$mail->Subject   = 'Formulário - Contato';
$mail->Body      = $emailText;
$mail->AddAddress( '[email protected]' );
$retorno = $mail->Send();

$mail2 = new PHPMailer();
$mail2->CharSet = 'UTF-8';
$mail2->From      = '[email protected]';
$mail2->FromName  = 'Contato - JF Security';
$mail2->Subject   = 'Folder Digital - JF Security';
$mail2->IsHtml(true);
$mail2->Body      = "Olá ". $_POST["nome"] . ", obrigado por entrar em contato conosco!<br> A apresentação está anexada nesse email conforme solicitado. Em caso de dúvida entre em contato <a href='https://api.whatsapp.com/send?phone=5511988003086&text=Ol%C3%A1!%20Eu%20solicitei%20a%20apresentação%20da%20da%20Musca%20e%20gostaria%20de%20algumas%20informa%C3%A7%C3%B5es!'>(11) 9 8800-3086</a>";
$mail2->AddAddress($_POST["email"]);
$mail2->AddAttachment("../PDF/folder-digital.pdf");
$retorno2 = $mail2->Send();
exit(json_encode($okMessage));
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
       'From: ' . $from,
       'Reply-To: ' . $from,
       'Return-Path: ' . $from,
    );
    
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

And this is the JS:

/*FORMULÁRIO FOLDER*/
$('#contact-form-folder').validator();

    $('#contact-form-folder').on('submit', function (e) {
        if (!e.isDefaultPrevented()) {
            var url = "../mail/contact-folder.php";

            $.ajax({
                type: "POST",
                url: url,
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify({
                    "to": $(this)[0][1].value,
                    "attachmentKey": "..PDF/novo-catalogo-de-produtos-e-servicos-jf-security.pdf",
                    "department": "marketing",
                    "data": {
                        "email": $(this)[0][1].value,
                        "nome": $(this)[0][0].value
                    }
                }),
                success: function (data)
                {
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;

                    if (data.success) {
                        messageAlert = 'alert-success';
                        messageText = 'Sua mensagem foi enviada com sucesso!';
                    }else{
                        messageAlert = 'alert-danger';
                        messageText = 'Sua mensagem não pode ser enviada...';
                    }
                }
            });
            return false;
        }
    })
/*FIM FORMULÁRIO FOLDER*/

  • What is this plugin .validator()?

  • So you want to fire the ajax, but not get off the screen is this ?

  • The "Validator" is a plugin part... I’ve done the tests and the problem is not in it.

  • I just want a "Your message has been sent!" message to appear just below the "Send!" button"

1 answer

0

It’s moving why you declared an action if you have using php on the same page only take the action if not on the other page if all goes well put a header('location: destino.php');

NOTE: Change destination to where it has to go...

Browser other questions tagged

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