User feedback

Asked

Viewed 32 times

0

I have a php method that performs a certain task and then I need to give a successful/error feedback to the user and return to my index.php. This feedback can occur both before returning and in my own index.php. My current scenario is as follows::

$conexao->setSQL("INSERT INTO tab VALUES ('x');
$resultado = $conexao->Executar();  


$erroRegistros = $totalRegistros - $adicRegistros;

if(!$resultado){
   die("erro in uploading the file".  mysql_error());
} else{
    // FEEDBACK NECESSÁRIO E RETORNO AO MEU INDEX
    voltarIndex();
}    

In this class, there are only code in php. Already in my index.php there are html, javascript and even php.

For questions, my method backIndex() follows below:

function voltarIndex() {
    header("Location: index.php");
}

Could someone help me ?

  • The index.php is a form?

  • my home page, containing several buttons, tables and the like

  • If possible post the part of the form responsible for sending the post, then I can supplement my reply with javascript.

1 answer

1


You can use XMLHttpRequest to make a request without leaving the page and at the end of the page, display a message to the user; or you can use Sessions to save a message and return to index.php, display the message.

Example with Sessions:

add-record.php

<?php

session_start();

$conexao->setSQL("INSERT INTO tab VALUES ('x')");
$resultado = $conexao->Executar();  


$erroRegistros = $totalRegistros - $adicRegistros;

if(!$resultado){
    $_SESSION["feedback"] = "Erro in uploading the file" . mysql_error();
} else{
    $_SESSION["feedback"] = "Digite sua mensagem aqui";
}

voltarIndex();

index php.

<?php session_start(); ?>
<!DOCTYPE hml>
<html>
    <head>
        <title>Title of the document</title>
    </head>

    <body>
        Página inicial
        Diversos botões
        tabelas
        afins

        <div class="msg"><?php echo (isset($_SESSION["feedback"])) ? $_SESSION["feedback"]; unset($_SESSION["feedback"]) : "" ?></div>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </body>
</html>

Remembering that session_start(); should stay on top.

Browser other questions tagged

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