How to alert the form correctly?

Asked

Viewed 345 times

4

I have a contact form, so when filled out it is not displaying the failed send alert or successfully sent, it is redirecting to a search page, here my script:

<form class="form-inline" action="<? $PHP_SELF; ?>" method="post">
    <div class="form-group">
        <label for="nome">Seu nome (obrigatório):</label>
        <input type="text" class="form-control" id="nome" name="nome" placeholder="Digite aqui seu nome...">
    </div>
    <div class="form-group">
        <label for="email">Seu e-mail (obrigatório):</label>
        <input type="email" class="form-control" id="email" name="email" placeholder="Seu email...">
    </div>
    <div class="form-group">
        <label for="telefone">Seu telefone (obrigatório):</label>
        <input type="tel" class="form-control" id="telefone" name="telefone" placeholder="Seu telefone...">
    </div>
    <div class="form-group">
        <label for="assunto">Assunto (obrigatório):</label>
        <input type="text" class="form-control" id="assunto" name="assunto" placeholder="Digite aqui o assunto...">
    </div>
    <div class="form-group">
        <label for="mensagem">Sua Mensagem:</label>
        <textarea type="text" class="form-control" id="mensagem" name="mensagem" placeholder="Escreva aqui sua mensagem..." rows="5"></textarea>
    </div>
    <button type="submit" class="btn btn-default" name="btnEnviarMensagem">Enviar Mensagem</button>
</form>

<?
if (isset($_POST['btnEnviarMensagem'])){
    // Passando os dados obtidos pelo formulário para as variáveis abaixo
    $nome    = $_POST['nome'];
    $emailremetente    = trim($_POST['email']);
    $emaildestinatario = '[email protected]'; // Digite seu e-mail aqui, lembrando que o e-mail deve estar em seu servidor web
    $telefone = $_POST['telefone'];
    $assunto = $_POST['assunto'];
    $mensagem = $_POST['mensagem'];


    /* Montando a mensagem a ser enviada no corpo do e-mail. */
    $conteudo   = "Nome: $nome<br>";
    $conteudo  .= "Email: $emailremetente<br>";
    $conteudo  .= "Telefone: $telefone<br>";
    $conteudo  .= "Assunto: $assunto<br>";
    $conteudo  .= "Mensagem: $mensagem<br>";


    // O remetente deve ser um e-mail do seu domínio conforme determina a RFC 822.
    // O return-path deve ser ser o mesmo e-mail do remetente.
    $headers = "MIME-Version: 1.1\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "From: $emailremetente\r\n"; // remetente
    $headers .= "Return-Path: $emaildestinatario \r\n"; // return-path
    if(mail($emaildestinatario, $assunto,  $conteudo, $headers, "-r".$emaildestinatario)){
        // echo "<strong style='color: green'>"."Sua mensagem foi enviada com sucesso!"."</strong>";
        //  echo '<script type="text/javascript">document.form1.reset();</script>';
        echo '
            <script type="text/JavaScript">
            alert("Sua mensagem foi enviada com sucesso. Obrigado");
            location.href="andreyferraz.php";
            </script>';
    }else{
        echo '
        <script type="text/JavaScript">
        alert("Aconteceu um erro, tente novamente mais tarde");
        location.href="andreyferraz.php";
         </script>';
    }
}?>

When I click send it executes the Else of this condition:

if ($resultados->num_rows > 0) {
    while($linha = mysqli_fetch_array($resultados)) {
        echo utf8_encode("<strong>Nome: </strong>" ."<strong>". $linha['nome']."</strong>" . "</br>");
        print ("<strong>Endereço: </strong>" . $linha['endereco'] . "</br>");
        if (isset($_POST['cidade']) && $_POST['cidade'] === 'sao-gabriel-da-palha') {
            $fromPerson = 'São Gabriel da Palha';
            echo "<strong>Cidade: </strong>" . $fromPerson . "</br>";
        }
        print ("<strong>Telefone: </strong>" . $linha['telefone'] . "</br>");
        echo "<strong>email: </strong>" . $linha['email'] . "</br>"."<hr>";
        if (isset($_POST['palavra'])) { // remover acentos
            $palavra = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities(strtolower(trim($_POST['palavra']))));
        }
        if (!empty($palavra) &&
            in_array($palavra, array('andrey ferraz', 'andrey', 'ferraz', 'andrey martins ferraz'))){
            require 'andreyferraz.php';

        }
        if (!empty($palavra) &&
            in_array($palavra, array('melfort'))){
            require 'melfort.php';

        }

    } }else{
   echo "<h3 align='center'>Empresa ainda não cadastrada!</h3>";
}
  • That second snippet of code you sent is being used where ?

  • in fact the mistake is in these if chained with while and Else, ta kind of a fruit salad that

  • @Victor what you suggested that I do, I also think that’s the problem, always when I click SEND ends up displaying the LSE, "unregistered company"

  • this code is all on a single page ?

  • No, the condition is on another page, so I’m making use of Require

1 answer

1


Eai friend, I found some details in the code that could be causing these errors.

Initially in:

 <form class="form-inline" action="<? $PHP_SELF; ?>" method="post">

Two Points. Must be used:

<?=$PHP_SELF?> 

to textualize a variable in output, in addition to:

<form class="form-inline" action="" method="post">

It would already be able to do the job, because it has no address url, it is redirected to the same page

Then I see another problem in:

<? 
if (isset($_POST['btnEnviarMensagem'])){

Currently PHP initializes with the tag

<?PHP

What may be causing conflict within decisions.

I say this for the following.

I copied your code: I changed the decisions to if(true).. I commented on the Javascript redirect lines

I tested: The alert() was called in both cases of the decision: TRUE and FALSE, and also in the html content your PHP script appeared as text.

I modified of

<?
//para 
<?php

The alert was called only once and this time no PHP script appeared on the screen.

Try this, this may be the problem, I know that not new versions php do not start with the simple tag. Me version of PHP: 5.2

  • Unfortunately that’s not it, I’ve made the changes here that you suggested, it’s running an I from another function when I click send, I know why this is happening!

  • Well let’s go then: 1) Is he redirecting to another page? 2) Is this happening via javascript or php? 3) before it redirects to the PHP email function or after?

  • Yes, it is redirecting and executing an Else from another function, printing the message Unregistered company

  • it is probably a problem in the database query so, which is not returning any results, of a revised one in it.

  • More see well, to make the sending of the email I do not use anything of the database bro

  • @Wpfan separate php from the form with php that will send the email.. in the action put send.email.php and put the sending code there..

Show 1 more comment

Browser other questions tagged

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