How to make an "Alert" in PHP?

Asked

Viewed 96,354 times

8

I’m making an email form in PHP, and I’m trying to make one alert when the client clicks to send a validation.

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

Is this code right?

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

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

header('location: index.php');
  • 3

    PHP runs on the server, JS Alert runs on the client. It would be nice to relida the basic concept of what is PHP, web server and internet browser, to facilitate the logic when doing the programming itself.

6 answers

9


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

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

I put in the Github for future reference.

To see working:

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

Obviously simplified, you will have to assemble a minimum page to perform this.

4

Instead of using Alert, you could do it another way. If the email is sent correctly, a div with the "success" CSS class will appear informing you of the submission and the clean form fields after sending. If an error occurs, a div appears with the "error" class stating that it could not be sent.

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

?>

3

About your code the case is simple, only missing a quote

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

Loveliness?

3

A possible solution, which merges the proposals of the other answers, is to assemble a minimum HTML, but using a META to refresh it 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 tag <a>, but in these cases it is interesting to keep, so that in case there is a problem in the refresh, the user is not left without knowing what to do.

Now, I think MUCH better a simpler thing. Since it is to build an HTML, warn on 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>';
?>

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 enjoy javascript without even opening the javascript tag:

Then you can try too:

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

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

header('location: index.php');

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

Then try using the rewriting inside the script

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

echo "<script>alert('Email enviado com Sucesso!);location.href=\"index.php\";</script>";
  • that way it didn’t roll ;/

  • I edited it, check it out....

  • @Staciodifabio looks and see if it worked....

  • Oops, dude hasn’t worked yet, the code with the Alert it redirects to the index, but it doesn’t show up without the Alert.

0

You can do a validation in the form:

<script>

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

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

If in the end everything goes okay, 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>

Browser other questions tagged

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