Contact Form updates on the same page

0

The goal is to put the same e-mail capture form (fields: name, e-mail, city and state) in all 20 pages of the site, and then, after the alert that the message was sent , stay on the same page where it was used. If it was used on the Who We Are page, after the alert, this page is the one that will be reloaded. If the form was used in Products, it is the product.html that is reloaded. And so on all 20.

File (send-to-mailing.php)

if(isset($_POST['acao']) && $_POST['acao'] == 'enviar'){
require("../funcoes/trata-dados.php");

$nome      = ValidarString($_POST['nome'], "Preencha o campo NOME COMPLETO");
$email     =  ValidarEmail($_POST['email']);
$cidade    = ValidarString($_POST['cidade'], "Preencha o campo CIDADE");
$estado    = ValidarString($_POST['estado'], "Preencha o campo ESTADO");

$msg = "Cadastro para mailing através do <strong>site</strong>:<br /><br /> 
        <strong>> Nome:</strong> $nome <br /><br />
        <strong>> E-mail:</strong> $email <br /><br />
        <strong>> Cidade/Estado:</strong> $cidade/$estado <br /><br />
        <strong>> Enviado em:</strong> ".date("d-M-Y  H:i");

        $destino = "[email protected]";
        $assunto = "$nome entrou em contato pelo site";

        if(EnviarEmail($destino, $email, $assunto, $msg)){
            echo '<script type="text/javascript">
                    alert("'.$nome.', sua mensagem foi enviada com sucesso!")
                    window.location = "???";
                    </script>';

        }else{
            echo '<script type="text/javascript">
                    alert("'.$nome.', sua mensagem ainda não foi enviada!")
                    </script>';
    }
}

Is there a command to do the "reload" page in "window.location" itself? It is worth mentioning that if you remove the line from the "window.location", the site reloads trying to open the file that treats the data (www.site.com/trata-dados.php) as follows:

function TratarDados($str){
            $str = trim($str);
            $str = strip_tags($str);
            $caracters = array("&", "%");
            $str = str_replace($caracters, "", $str);
            return $str;
    }

    function ValidarString($string, $erro){
            $string = TratarDados($string);
            if(empty($string)){
                echo '<script type="text/javascript">alert("'.$erro.'")</script>';
                echo '<script type="text/javascript">history.back()</script>';
                exit;
            }
            return $string;
    }

    function ValidarEmail($email){
            $email = TratarDados($email);
            if(empty($email)){
                echo '<script type="text/javascript">alert("Preencha o campo E-MAIL")</script>';
                echo '<script type="text/javascript">history.back()</script>';
                exit;
            }
            elseif(substr_count($email, "@") !== 1 || substr_count($email, ".") == 0){
                echo '<script type="text/javascript">alert("Preencha com um E-MAIL válido")</script>';
                echo '<script type="text/javascript">history.back()</script>';
                exit;
        }
        return $email;
    }

    function EnviarEmail($destino, $email, $assunto, $msg){
            $headers  = "MIME-Version: 1.1\n";
            $headers .= "Content-Type:text/html; charset=utf-8\n";
            $headers .= "From: $destino\n";
            $headers .= "Return-Path: $destino\n";
            $headers .= "Reply-To: $email\n";
            return mail($destino, $assunto, $msg, $headers);
    }
    
asked by anonymous 15.06.2016 / 19:41

1 answer

0

Make an ajax, if you're using jquery, it looks like this:

    $.ajax({
        url      : 'URL_QUE_TRATA_DE_ENVIAR_EMAIL',
        type     : 'post',
        data     : {'NOME':$("input[name=nome]").val(),'EMAIL':$("input[name=email]").val(),'CIDADE':$("input[name=cidade]").val(),'ESTADO':$("input[name=estado]").val()},
        dataType : 'json',
        error    : function() {
          alert('Email não foi enviado com sucesso');
        },
        success  : function(data) {
          alert("Email enviado com sucesso !");
        }
    });

And in the url in PHP, do the same check, if you want to return something different or an object, return in json and use the function data 'date' that would store the return. That's the right way. You can refresh in success to simulate a page load.

    
15.06.2016 / 21:27