I need to save the value of a form after refresh

Asked

Viewed 122 times

1

Good , I have a page with form that contains some data that I show users, so when you search it changes page. I wanted the information that the user was searching to be "saved" in case he wanted to take that information to Excel , do not have to put the search information again. Follow my code for better viewing .

Php query.

<?php include("cabecalho_sg.php")?>
    <br>
    <!-- ##### Breadcrumb Area Start ##### -->
    <div class="mag-breadcrumb py-3">

    </div>
    <!-- ##### Breadcrumb Area End ##### -->
    <div class="container">

        <form method="post" action="result_sg.php">
            <div class="row align-items-center justify-content-center">
                <div class="row align-items-center justify-content-center">

                    <b>Filial: &nbsp &nbsp </b>
                    <select name="estado_inicial" id="estado_inicial">
                                            <option value="1">Bahia</option>
                                            <option value="2">São Paulo</option>
                                            <option value="3">Rio de Janeiro</option>

                    </select>

                    &nbsp até &nbsp

                    <select name="estado_final" id="estado_final">
                                            <option value="1">Bahia</option>
                                            <option value="2">São Paulo</option>
                                            <option value="3">Rio de Janeiro</option>

                    </select>

                    &nbsp &nbsp &nbsp <b>Data:</b> &nbsp
                    <input type="date" id="data_inicio" name="data_inicial"> &nbsp até &nbsp

                    <input type="date" id="data_final" name="data_final"> &nbsp &nbsp

                    <button type="submit">&nbsp Consultar &nbsp </button>&nbsp


                </div>
            </div>

        </form>
    </div>
    <br>
    <!-- ##### Breadcrumb Area End ##### -->
    <?php include("rodape.php")?>

php result.

<?php
    define('DB_HOST'        , "");
    define('DB_USER'        , "");
    define('DB_PASSWORD'    , "");
    define('DB_NAME'        , "");
    define('DB_DRIVER'      , "");

    require_once "Conexao.php";

try{

  //inicia a conexão
   $Conexao     = Conexao::getConnection();

   $filial_inicial = $_POST['estado_inicial'];
   $filial_final = $_POST['estado_final'];
   $data_inicial = $_POST['data_inicio'];
   $data_final = $_POST['data_final'];


  //faz a consulta na tabela procurando palavras do form
   $query       = $Conexao->query ("EXEC PROCEDURE '$data_inicial','$data_final','$filial_inicial','$filial_final'");
   $teste       = $query->fetchAll();




  }catch(Exception $e){
        echo $e->getMessage();
        exit;
    }
?>
<!DOCTYPE html>
<html lang="pt-br">

<head>
    <?php

  // A sessao precisa ser iniciada em cada página diferente
  if (!isset($_SESSION)) session_start();
    $setor_necessario = 55;

  // Verifica se nao há a variável da sessao que identifica o usuário
  if (!isset($_SESSION['email']) OR ($_SESSION['nivel'] != $setor_necessario)  ) {
      // Destrói a sessao por segurança
      session_destroy();
      // Redireciona o visitante de volta pro login
      header("Location: index.php"); 
      exit;
  }

  ?>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- The above 4 meta tags *must* come first in the head; any other head content must come *after* these tags -->

        <!-- Title -->
        <title>Central de Relatorios | Moinho Canuelas</title>

        <!-- Favicon -->
        <link rel="icon" href="img/ll.ico">

        <!-- Stylesheet -->
        <link rel="stylesheet" href="style_busca.css">
        <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.quicksearch/2.3.1/jquery.quicksearch.js"></script>

</head>

<body>

    <!-- Preloader -->
    <div class="preloader d-flex align-items-center justify-content-center">
        <div class="spinner">
            <div class="double-bounce1"></div>
            <div class="double-bounce2"></div>
        </div>
    </div>

    <!-- ##### Header Area Start ##### -->
    <header class="header-area">

        <!-- Navbar Area -->
        <div class="mag-main-menu" id="sticker">
            <div class="classy-nav-container breakpoint-off">
                <!-- Menu -->
                <nav class="classy-navbar justify-content-between" id="magNav">
                    <!-- Nav Content -->
                    <div class="nav-content ">

                        <table>
                            <tr>

                                <th scope="col">
                                    <form method="post" action="result_sg.php">
                                        <b>Filial: &nbsp &nbsp </b>
                                        <select name="estado_inicial" id="estado_inicial">
                                            <option value="1">Bahia</option>
                                            <option value="2">São Paulo</option>
                                            <option value="3">Rio de Janeiro</option>

                    </select>

                                        &nbsp até &nbsp

                                        <select name="estado_final" id="estado_final">
                                            <option value="1">Bahia</option>
                                            <option value="2">São Paulo</option>
                                            <option value="3">Rio de Janeiro</option>

                    </select>
&nbsp &nbsp &nbsp &nbsp
                                        <label for="date">Data </label>
                                        <input type="date" id="data_inicio" name="data_inicio"> até
                                        <input type="date" id="data_final" name="data_final"> &nbsp &nbsp
                                        <button type="submit">&nbsp Consultar &nbsp </button>&nbsp
                                        <button type="submit" formaction="excel.php">&nbsp Gerar Excel &nbsp </button>
                                        <button type="submit" formaction="inicio.php">&nbsp Voltar &nbsp </button>

                                        &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
                                        <input name="consulta" id="txt_consulta" placeholder="Digite para Pesquisar" type="text">
                                </th>
                            </tr>

                            </form>
                        </table>
                    </div>
                    <!-- Nav brand -->
                    <a href="inicio.php" class="nav-brand"><img src="img/logo.png" alt=""></a>

                </nav>
            </div>
        </div>

    </header>
    <!-- ##### Header Area End ##### -->

    <div class="table-responsive">
        <table id="tabela" class="table-bordered">
            <thead>
                <tr>
                    <th scope="col">
                        <center><b>Filial</b></center>
                    </th>

                    <th scope="col">
                        <center><b>Entrega</b></center>
                    </th>


                </tr>
            </thead>
            <?php
        foreach($teste as $CLIENTE) {
    ?>
                <tr>
                    <td>
                        <center>
                            <?php echo $CLIENTE['FILIAL']; ?>
                        </center>
                    </td>
                    <td>
                        <center>
                            <?php echo $CLIENTE['ENTREGA']; ?>
                        </center>
                    </td>
                </tr>
                <?php
        }
    ?>
        </table>

        <?php include("rodape.php")?>

That is, the person is redirected to a page with results , however I would like the parameters that she did the search to be saved so that if she wants to click on generate excel do not have to put the parameters again.

1 answer

1


You can use $_SESSION from PHP, it would look something like this:

<?php
    //na pagina onde é enviado os valores
    //Ativa as sessões. isso requer em todas que voce queira mostrar esses valores.
    session_start();
    if(isset($_SESSION['Var'])){
        //caso a sessão não exista ele cria e define como array
        $_SESSION['Var'] = array();
    }
    //Pega os valores enviados e adiciona ao array
    $_SESSION['Var'][] = $_POST['valor_para_adicionar'];

    //na pagina onde você quer que sejam mostrado os valores registrados
    echo '<input type="text" list="historico"/>';
    //verifica se existe historicos anteriores
    if(isset($_SESSION['Var'])){

        //Na pagina que voce queria mostrar o historio você pode usar o datalist fazendo referencia ao input
        echo '<datalist id="historico">';
        //lista os valores da session como opções
        foreach ($_SESSION['Var'] as $historico) {
            echo '<option value="'.$historico.'">';
        }
        echo '</datalist>';
    }
?>

Browser other questions tagged

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