How to make an "alert" in PHP?

6

I'm doing an e-mail form in PHP, and I'm trying to make a alert when the client clicks submit a validation.

The validation seems to work because it redirects to the page I want, but does not show alert .

Is this code correct?

mail($email,$assunto,$mens,$headers);

echo  "<script>alert('Email enviado com Sucesso!);</script>";

header('location: index.php');
    
asked by anonymous 14.03.2015 / 18:07

6 answers

8

The alert is not a good solution to show some message to the user in the browser but I will answer what you asked:

mail($email,$assunto,$mens,$headers);
echo "<script type='javascript'>alert('Email enviado com Sucesso!');";
echo "javascript:window.location='index.php';</script>";

To see it working:

javascript:alert('Email enviado com Sucesso!');
javascript:window.location='index.php';

Obviously I've simplified it, you'll have to set up a minimal page to do this.

    
14.03.2015 / 18:15
4

Instead of using alert, you could do it another way. If the email is sent correctly, a div with the CSS class "success" will appear notifying the submission and fields of the form clean after submission. If an error occurs, a div appears with the class "error" informing that it was not possible to send it.

It would look something like this:

<?php

if ($_POST)
{
    $envioEmail = mail($email,$assunto,$mens,$headers);

    if ($envioEmail)
    {
    ?>
        <div class="sucesso">E-mail enviado com sucesso!</div>
    <?php
    }
    else
    {
    ?>
        <div class="erro">Erro no envio do e-mail.</div>
    <?php
    }
}

?>
    
20.03.2015 / 13:41
2

A possible solution, which merges the proposals of the other answers, is to assemble a minimal HTML, but using a META for the refresh take a few seconds without disturbing ALERT:

<?php
   mail($email,$assunto,$mens,$headers);
   echo '<!DOCTYPE html>';
   echo '<html xmlns="http://www.w3.org/1999/xhtml">';
   echo '<head>';
   echo '   <meta http-equiv="refresh" content="5; url=http://example.com/index.php">';
   echo '</head>';
   echo '<body onload="alert('+"'"+'Email enviado com Sucesso!'+"'"+');">';
   echo '<a href="http://example.com/index.php">click!</a>';
   echo '</body>';
   echo '</html>';
?>

You can remove the <a> tag, but in these cases it is interesting to keep, in case there is a problem in refresh , the user does not know what to do.

Now, I think MUCH better a simpler thing. Since it is to create an HTML, please notify the page itself:

<?php
   mail($email,$assunto,$mens,$headers);
   echo '<!DOCTYPE html>';
   echo '<html xmlns="http://www.w3.org/1999/xhtml">';
   echo '<head>';
   echo '   <meta http-equiv="refresh" content="10; url=http://example.com/index.php">';
   echo '</head>';
   echo '<body>';
   echo '<p>Seu email foi enviado com sucesso.</p>';
   echo '<a href="http://example.com/index.php">Prosseguir</a>';
   echo '</body>';
   echo '</html>';
?>
    
14.03.2015 / 20:25
2

On your code, the case is simple, just missing a quotation mark

echo  "<script>alert('Email enviado com Sucesso!');</script>";

Beauty?

    
26.06.2017 / 16:51
1

Try this:

mail($email,$assunto,$mens,$headers);

header("location:javascript:alert(\"Email enviado com Sucesso!\");location.href=\"index.php\";");

And analyzing your code, I realized that you want to execute javascript without even opening the javascript tag:

Then you can also try:

mail($email,$assunto,$mens,$headers);

echo "<script>alert('Email enviado com Sucesso!);</script>";

header('location: index.php');

And this would cause the header to not run, because if you want to use the header you would have to put it before sending any response ....

Then try redirecting within the script

mail($email,$assunto,$mens,$headers);

echo "<script>alert('Email enviado com Sucesso!);location.href=\"index.php\";</script>";
    
14.03.2015 / 18:14
-1

You can do a validation on the form:

<script>

function checar()
{
  If (form.qualquercoisa.nome.value=="")
{
   alerta("preencher o campo");
}
else
{
  function enderecoform();
}

Make a function to check the address and then one to check the email.

If at the end everything is ok, you make a alert to confirm the redirect with:

alert("sua página será redirecionada");
window.location("endereço da página");

Ex.

<Form name="qualquer coisa"action="arquivo php" onSubmit="return função checar();">

nome<input type ="texto" name ="nome">
nome<input type ="texto" name ="endeco">
nome<input type ="texto" name ="e-mail">
<Input type ="button">Enviar<\button>
    
13.08.2018 / 19:03