Contact Form updates on the same page

Asked

Viewed 87 times

0

The goal is to put in all 20 pages of the site the same form of capturing emails (fields: name, email, city and state) that works in all and, after the alert that the message was sent, stay on the same page where it was used. If 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.html product that is reloaded. And so in 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 in "window.Location" the page reload "on itself"? It is worth noting that if you take the "window.Location" line, the site reloads trying to open the file that handles the data (www.site.com/treats-data.php) that follows below:

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);
    }

1 answer

0

Do an ajax, if you are using jquery, it will look 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 PHP url, do the same check, if you want to return something different or an object, return in json and use the Function sucess date that stores the return. This is the most correct way. You can give a refresh in sucess, to simulate a page loading.

  • Leonardo, I’m beginning to understand programming! Then I had the nerve to come in here to ask about the form script I found on the internet and adapted. Surely your solution is the best, but technically I still don’t know how to execute it! I don’t know, for example, if ajax is to replace the command lines of the alert in the file above. Sorry for your time, but I thought a magic word in window-Location = "???" would solve that question. Great that I have a lot to learn! I will use "contact.php" to reload. Thank you!

Browser other questions tagged

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