Display an ALERT and redirect the page

Asked

Viewed 5,140 times

0

I have a page where the form is with an action that sends the form information to another page.

I would like a confirmation (or negation) message to be displayed after the registration has been entered and, after clicking the OK button, a redirection to the original page.

The form page looks like this:

<form id="frmCad" name="frmCad" method="post" action="../inc/processa.php?modo=incluir&fnc=<?php echo $fnc; ?>&ans=<?php echo $linha; ?>">

The proxessa.php file looks like this:

$fnc        = $_GET['fnc'];
$linha      = $_GET['ans'];

if($modo == "incluir"){

$sql = "INSERT INTO `pe_premorc` (etc)

$linhafec       = mysqli_affected_rows($conexao);
if($linhafec == 1){
    echo "<script>alert('Premissa incluída com sucesso');</script>";
    header('Location: ../views/premorc.php?fnc=$fnc&ans=$linha');
} else {
    echo "<script>alert('Ocorreu um erro. A premissa não foi incluída');</script>";
    header('Location: ../views/premorc.php?fnc=$fnc&ans=$linha');
}

But I’m not getting the desired effect. Either display the message and stay on the same page, or neither display the message and redirect with the wrong parameters, type http://localhost/final/views/premorc.php? fnc=$fnc&ans=$line

  • I come to say that I tested the first one and, this one worked, it worked.

1 answer

2


Try this way by placing the redirect in javascript as well:

echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Ocorreu um erro. A premissa não foi incluída')
window.location.href='../views/premorc.php?fnc=$fnc&ans=$linha';
</SCRIPT>");

Example:

$fnc        = $_GET['fnc'];
$linha      = $_GET['ans'];

if($modo == "incluir"){

$sql = "INSERT INTO `pe_premorc` (etc)"

$linhafec       = mysqli_affected_rows($conexao);
if($linhafec == 1){
    echo ("<script>
        window.alert('Premissa incluída com sucesso')
        window.location.href='../views/premorc.php?fnc=$fnc&ans=$linha';
    </script>");
} else {
    echo ("<script>
        window.alert('Ocorreu um erro. A premissa não foi incluída')
        window.location.href='../views/premorc.php?fnc=$fnc&ans=$linha';
    </script>");
}

You can do it this way too:

Send message as parameter:

$mensagem = 'Ocorreu um erro. A premissa não foi incluída';
header("Location: ../views/premorc.php?fnc=$fnc&ans=$linha&msg=$mensagem");

and on the premorc.php page shows the message:

<?php 
if(isset($_GET['msg'])){
    echo "<script>alert('" . $_GET['msg'] . "');</script>";
}
?>
  • Thanks for the Wictor reply, but it is returning the error Parse error: syntax error, Unexpected '=' in D: xampp htdocs final inc processes.php on line 41 which is where the window.Location is =

  • I put a new way to make see.

  • And I also changed the first one to correct this mistake

Browser other questions tagged

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