How to return an error after sending a form?

Asked

Viewed 2,294 times

2

How do I return the echo "Deu Erro!"? on my page?

//page-2.php

<form method="post" id="formulario" action="acoes/enviar-formulario.php">
   //codigo aqui
</form>

within the enviar-formulario.php there is an update:

$update = mysql_query("UPDATE tabela set nome=\"algo\" WHERE ID=$id");

if($update === false){   
    echo "Deu Erro!";
    header("location: ../pagina-2.php");
} else{
    header("location: ../pagina-3.php");
}
  • Remove the header() or error via get

4 answers

3


One option is to take the error message and pass it via get. Do not use the mysql_* functions they will be removed in php7.

$sql = "UPDATE tabela set nome='algo' WHERE ID = $id";
$update = mysql_query($sql) or die(mysql_error());

if($update === false){   
    $erro = "Deu Erro!";
    header("location: ../pagina-2.php?erro=$erro");
} else{
    header("location: ../pagina-3.php");
}

1

You can do this using a GET parameter in the URL:

<?php
if (isset($_GET['erro']) && $_GET['erro'] == '1') {
   echo "Deu Erro!";
}
?>
<form method="post" id="formulario" action="acoes/enviar-formulario.php">
   //codigo aqui
</form>

And in the file send-form.php would look like this:

$update = mysql_query("UPDATE tabela set nome=\"algo\" WHERE ID=$id");

if($update === false){   
    header("location: ../pagina-2.php?erro=1");
} else{
    header("location: ../pagina-3.php");
}

0

Look, a recommendation. To set error messages, use sessions or even cookies. In addition to being viable for this task, they are easy to handle when it comes to storing and displaying simple error messages.

Example:

<?php

//-Simular banco de dados
$database = array("id" => 1,
                        "usuario" => "Edilson",
                        "password" => "palavrapasse",
                        "morada" => "Desconhecido_"        
                 );
             
if(isset($_POST["cadastrar"])){
    $nome = isset($_POST["nome"]) ? (string) $_POST["nome"] : NULL;    
    $password = isset($_POST["password"]) ? (string) $_POST["password"] : NULL;
    
    if(in_array($nome, $database) && in_array($password, $database)){
        setcookie("mensagem", "Login efectuado", time()+2);
        //$_SESSION["mensagem"] = "Login efectuado";
        // Sessoes do usuario...
        // Outros
        header("Location: logado.php");    
        exit();
    } else {
        //$_SESSION["erro"] = "Usuario nao existe";    
        setcookie("erro", "usuario nao existe", time()+2);
        header("Location: erros.php");
        exit();    
    }
}                 


?>

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>{ Erros }</title>
    </head>
    
    <body>
    <!-- Erros !-->
    <div id="erros">
    <?php  if(isset($_COOKIE["erro"])){ echo $_COOKIE["erro"]; $_COOKIE["erro"] = NULL; }  ?>
    <?php //echo "ola"; ?>
    </div>
    <!-- Erros !-->
    <form method="POST" action="">
        <input type="text" name="nome" placeholder="Digite o nome"/><br/><br/>
        <input type="password" name="password" placeholder="Digite senha"/><br/><br/>
        <input type="submit" name="cadastrar" value="Cadastrar"/>
    </form>
    </body>
</html>

In this script for example, I used a array to simulate the database, although it works normally.

The variables of session or cookies have lifetime, and can be used on any page, provided they have been set, as I did in the example above. Although the script PHP be unified with the html form nothing changes, even if they were in different files, the message would still be printed without any problem.

0

Try to pass a get or create a session variable and within the answer file you test. It is good to also use the mysql_affected_rows which returns the number of records reached by the query.

$update = mysql_query("UPDATE table set name= "something " WHERE ID=$id");

if($update === false){   
    echo "Deu Erro!";
    header("location: ../pagina-2.php?mensagem=erro");
} else{
    header("location: ../pagina-3.php?mensagem=OK");
}

Browser other questions tagged

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