how to return an error after submitting a form?

2

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

// page-2.php

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

Within 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");
}
    
asked by anonymous 07.10.2015 / 14:59

4 answers

3

One option is to get the error message and pass it via get. Do not use the mysql_ * functions they will remove 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");
}
    
07.10.2015 / 15:06
1

You can do this by 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 send-form.php file it 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");
}
    
07.10.2015 / 15:10
0

Try to pass a get or create a session variable and within the response file you test. It's also good to use the mysql_affected_rows that 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");
}
    
07.10.2015 / 15:05
0

Look, a recommendation. To set error messages, use sessions or even cookies . In addition to being feasible for this task, they are easy to manipulate 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 an array to simulate the database, although it works normally.

Session or cookies variables have a lifetime, and can be used on any page as long as they have been defined, as I did in the example above. Although the PHP script is unified with the HTML format, nothing changes, even if they were in different files, the message would continue to print without any problems.     

07.10.2015 / 17:46