Message to user after header change

Asked

Viewed 1,898 times

2

I have a small problem with PHP, I need to redirect the user, but I wanted to notify you of what happened, I redirect you with the header(), but if I try to send something before the header failure, any hint as to how I can send a message to the usuario and modify the header? I tried to change the header before sending the message, but in that case the message was not sent.

Here’s the code I made, I’m using Mysql and wanted to notify the user when it was registered and when there was a failure:

<?php
    error_reporting(E_ALL);ini_set('display_errors', 1);
    $host = "localhost:8889";
    $user = "root";
    $pass = "root";
    $banco = "ArrayEnterprises";
    $conexao = mysqli_connect($host, $user, $pass, $banco);
    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $senha = md5($_POST["senha"]);
    $img = $_POST["img"];
    $inserir = "INSERT INTO Usuario (nome, email, senha, foto) VALUES('{$nome}', '{$email}', '{$senha}', '{$img}')";
     if($query = mysqli_query($conexao, $inserir)){
        if(mysqli_affected_rows($conexao) > 0){
            header("location: index.html");
            echo"<script language='javascript' type='text/javascript'>alert('Cadastrou!');</script>";
        }
    } else {
        echo"<script language='javascript' type='text/javascript'>alert('houve um erro');</script>";
        header("location: cadastro.html");
    }
    mysqli_close($conexao);
?>

2 answers

3

A first option would be to consider using sessions, storing the message on the source page, and capturing that message on the same page, or on another page, after the recirect.

<?php
# pagina principal
session_start();

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    $_SESSION['msg'] = 'Redireccionado com sucesso';
    header("Location: redirect.php");
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    $_SESSION['msg'] = 'Erro';
    header("Location: redirect.php");
} else {
    print "</p>Clique num dos links abaixo</p>";
}
#capturar mensagem
if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
    print "<script>alert(\"{$_SESSION['msg']}\")</script>";
}

?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>

You could also use javascript itself to redirect after showing Alert, by doing the following:

<?php

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    print "<script>alert('sucesso');</script>";
    print "<script>location.href='redirect1.php';</script>";
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    print "<script>alert('erro');</script>";
    print "<script>location.href='redirect1.php';</script>";
} else {
    print "</p>Clique num dos links abaixo</p>";
}


?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>

In a third option, you could still print the message in that same script, and then redirect:

<?php

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    print "sucesso";
    print "<script>setTimeout(\"location.href='redirect1.php'\", 1000);</script>";
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    print "erro";
    print "<script>setTimeout(\"location.href='redirect1.php'\", 1000);</script>";
} else {
    print "</p>Clique num dos links abaixo</p>";
}


?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>

0

As the friend said earlier use:

$_SESSION['mensagem'] = 'mensagem';

Then do your header, and on the redirected page show the Session:

echo $_SESSION['mensagem']

Browser other questions tagged

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