Recover field without include/require

Asked

Viewed 89 times

0

I have a report created in mPDF that performs a select through the following function that is found on the page "registra_pessoa.php":

include_once("registra_pessoa.php");
$pessoas = filtraPessoas();

Follows function:

function filtraPessoas(){
    $grupo = '';
    $banco = abrirBanco();
    if(isset($_POST['nomefiltrar'])){
    $teste = $_POST['nomefiltrar'];
    }else{
        $teste = '';
    }
    $sql = "SELECT id, nome, DATE_FORMAT(nascimento, '%d/%m/%Y') AS data_formatada, endereco, telefone FROM pessoa WHERE nome LIKE '%$teste%' ";
    $resultado = $banco->query($sql);
    $banco->close();
    while($row = mysqli_fetch_array($resultado)){
        $grupo[] = $row;
    }
    return $grupo;
}

Through the "name filter" field, the report should filter, however, I realized that the variable '$test' is not receiving the value contained in the field.

I tried to include once('query clients.php') (page containing this field), but returns error because mPDF uses its own tags from , etc:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\despesas\consulta_clientes.php:180) in C:\xampp\htdocs\despesas\MPDF57\mpdf.php on line 7447

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\despesas\consulta_clientes.php:180) in C:\xampp\htdocs\despesas\MPDF57\mpdf.php on line 1736
mPDF error: Some data has already been output to browser, can't send PDF file

Follows form:

<div class="container">
        <form hidden name="filtroClientes" action="consulta_clientes.php" method="POST">
            <div class="form-group row">
                <label for="nomefiltrar" style="color:#E4CDAC; font-size: 17px; font-family:Arial">Nome</label>
                <p>
                <input type="text" class="teste" id="nomefiltrar" name="nomefiltrar" />
            </div>

        <input type="hidden" name="acao" value="filtrar" />
            <button type="submit" class="btn btn-custom btn-roxo">Filtrar</button>
    </div>
</form>

@Edit:

consulta_clientes.php :

<?php include_once("registra_pessoa.php");
    $grupo = filtraPessoas();



    if(!$_SESSION['usuario']){
        header('Location: index.php?erro=1');
    }

?>

<!DOCTYPE HTML>
<html lang="pt-br">
    <head>



        <script type="text/javascript">
            function ocultaForm(tag){
                $(tag).show();
            }



        </script>

        <meta charset="UTF-8">

        <title>Despesas</title>

        <!-- jquery - link cdn -->

          <script src="https://code.jquery.com/jquery-3.2.1.min.js"
          integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
          crossorigin="anonymous"></script>

          <script type="text/javascript" src="js/jquery.mask.min.js"/></script>


        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

        <!-- bootstrap - link cdn -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link href="estilo.css" rel="stylesheet">
    </head>

    <body>

<!-- Static navbar -->
        <nav class="navbar navbar-fixed-top navbar-inverse navbar-transparente">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#barranavegacao">
                        <span class="sr-only">Alternar navegação</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <a href="home.php" class="navbar-brand">
                       <span class="img-logo">Despesas</span>
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="barranavegacao">
                    <ul class="nav navbar-nav">
                      <li><a href="home.php">Home</a></li>
                      <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Cadastro
                        <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                          <li><a href="inserir_pessoa.php">Cadastro de Clientes</a></li>
                          <li><a href="inserir_produto.php">Cadastro de Produtos</a></li>
                        </ul>
                      </li>
                      <li class="dropdown active">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Consultas
                        <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                          <li><a href="consulta_clientes.php">Consulta de Clientes</a></li>
                          <li><a href="consulta_produtos.php">Consulta de Produtos</a></li>
                          <li role="separator" class="divider"></li>
                          <li><a href="consulta_contas.php">Consulta de Contas a Pagar</a></li>
                          <li><a href="consulta_contas_receber.php">Consulta de Contas a Receber</a></li>
                      </ul>
                      <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Financeiro
                        <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                          <li><a href="inserir_conta.php">Contas a Pagar</a></li>
                          <li><a href="inserir_recebimento.php">Contas a Receber</a></li>
                        </ul>
                      </li>


                    </ul>

                      <ul class="nav navbar-nav navbar-right">
        <li>
          <a href="">Ajuda</a>
        </li>
        <li class="divisor" role="separator">
        </li>
        <li>
          <a href="sair.php">Sair</a>
        </li>
    </ul>
</div>



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


        <div class="container">
        <div class="thumb">
        <a href="pdf_clientes.php" target="_blank">
        <img src="imagens/printer.png">
        </a>
        <div class="desc"><span style="color:white">Imprimir Relatório</span></div>
        </div>

        <div class="thumb">
            <img id="filtraresultados" name="filtraresultados" src="imagens/search.png" style="cursor:pointer" "/>
        <div class="desc"><span style="color:white">Filtrar Resultados</span></div>
        </div>


        </div>


        <br>

        <div class="container">
        <form hidden name="filtroClientes" action="consulta_clientes.php" method="POST">

            <div class="form-group">
                <label for="nomefiltrar" style="color:#E4CDAC; font-size: 17px; font-family:Arial">Nome</label>
                <p>

                <input type="text" class="teste" id="nomefiltrar" name="nomefiltrar" />


            </div>

        <input type="hidden" name="acao" value="filtrar" />
            <button type="submit" class="btn btn-custom btn-roxo">Filtrar</button>
    </div>
</form>
<br>



        <script>
            filtraresultados.addEventListener("click", function() {
                ocultaForm('form');
            }, false);
        </script>

        <script>
            filtraresultados.addEventListener("click", function() {
                ocultaFormBotao('input');
            }, false);
        </script>






        <div class="container divconsultas">

                <table class="table">
            <thread>
                <tr>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Nome</td>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Data de nascimento</td>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Telefone</td>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Endereço</td>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Editar</td>
                    <td align="center" style="color:white; font-size: 17px; font-family:Tahoma">Excluir</td>
                </tr>
            </thread>
                <?php
                if (is_array($grupo) || is_object($grupo)){
                foreach($grupo as $pessoa){
                 ?>

                    <tr>
                        <td align="center" style="color:white; font-size: 15px; font-family:Tahoma"> <?=$pessoa["nome"]?> </td>
                        <td align="center" style="color:white; font-size: 15px; font-family:Tahoma"> <?=$pessoa["data_formatada"]?> </td>
                        <td align="center" style="color:white; font-size: 15px; font-family:Tahoma"> <?=$pessoa["telefone"]?> </td>
                        <td align="center" style="color:white; font-size: 15px; font-family:Tahoma"> <?=$pessoa["endereco"]?></td>
                        <td align="center" style="color:white; font-size: 15px; font-family:Tahoma">

                             <form align="center" action="alterar_pessoa.php" name="alterar" method="POST">
                                <input type="hidden" name="id" value=<?=$pessoa["id"]?>>
                                <input type="submit" class="btn editar cor" value="Editar" name="Editar" class="btn btn-default">
                             </form>

                        </td>
                        <td>

                            <form align="center" action="registra_pessoa.php" name="excluir" method="POST">
                                <input type="hidden" name="id" value=<?=$pessoa["id"]?>>
                                <input type="hidden" name="acao" value="excluir"/>
                                <input type="submit" class="btn editar excluir" onclick="msgSucesso()" value="Excluir" name="excluir" class="btn btn-default">
                            </form>

                        </td>
                    </tr>


                </div>

                    <script>
                        function msgSucesso(){
                            alert('Pessoa excluida com sucesso');
                        }
                    </script>


                <?php
                }
            }else{
                echo('Ainda não existem clientes cadastrados');
            }

                ?>

            </table>
        </div>




    </body>
</html>

Registra_pessoa.php:

<?php

session_start();


require_once('db.class.php');

if(!$_SESSION['usuario']){
    header('Location: index.php?erro=1');
}

if(isset($_POST['acao'])){
    if($_POST['acao'] == "inserir"){
    inserirPessoa();
    }
    if($_POST['acao'] == "alterar"){
        alterarPessoas();
    }
    if($_POST['acao'] == "excluir"){
        excluirPessoas();
    }
    if($_POST['acao'] == "filtrar"){
        filtraPessoas();
    }

}


function abrirBanco(){
    $conexao = new mysqli("localhost", "root", "", "despesas");
    return $conexao;
}

function valortotal(){
    $grupo = selectContas();
    foreach($grupo as $contas){
        $valortotal += $contas["valor"];
    }
}


function inserirPessoa(){

     $nome = $_POST['nome'];
     $datanascimento = $_POST['nascimento'];
     $endereco = $_POST['endereco'];
     $telefone = $_POST["telefone"];
     $id = $_POST['id'];


    $banco = abrirBanco();
    $sql = "INSERT INTO pessoa(nome,nascimento,endereco,telefone) VALUES ('$nome', '$datanascimento', '$endereco', '$telefone')";

    $banco->query($sql);
    $banco->close();

    header('Location: home.php');

}



function filtraPessoas(){
    $grupo = '';
    $banco = abrirBanco();
    $teste = $_POST['nomefiltrar'];

    $sql = "SELECT id, nome, DATE_FORMAT(nascimento, '%d/%m/%Y') AS data_formatada, endereco, telefone FROM pessoa WHERE nome LIKE '%$teste%' ";
    $resultado = $banco->query($sql);
    $banco->close();
    while($row = mysqli_fetch_array($resultado)){
        $grupo[] = $row;
    }
    return $grupo;
}

function selectIdPessoas($id){
    $banco = abrirBanco();
    $sql = "SELECT * FROM pessoa WHERE id = ".$id;
    $resultado = $banco->query($sql);
    $banco->close();
    $pessoa = mysqli_fetch_assoc($resultado);
    return $pessoa;
}

function alterarPessoas(){

     $nome = $_POST['nome'];
     $id = $_POST['id'];
     $datanascimento = $_POST['nascimento'];
     $endereco = $_POST['endereco'];
     $telefone = $_POST['telefone'];
     $id = $_POST['id'];
    $banco = abrirBanco();
    $sql = "UPDATE pessoa set nome = '$nome', nascimento = '$datanascimento', endereco = '$endereco', telefone = '$telefone' WHERE id= '$id' ";
    $banco->query($sql);
    $banco->close();

    header('Location: consulta_clientes.php');


}

function excluirPessoas(){

     $nome = $_POST['nome'];
     $id = $_POST['id'];
     $datanascimento = $_POST['nascimento'];
     $endereco = $_POST['endereco'];
     $telefone = $_POST['telefone'];
     $id = $_POST['id'];
    $banco = abrirBanco();
    $sql = "DELETE FROM pessoa WHERE id= '$id' ";
    $banco->query($sql);
    $banco->close();

    header('Location: consulta_clientes.php');


}


?>

pdf_clients.php:

<?php
    include_once("registra_pessoa.php");
    include("./MPDF57/mpdf.php");
    $nome_filtrar = (isset($_POST['nomefiltrar'])) ? $_POST['nomefiltrar'] : "teste";
    $pessoas = filtraPessoas();
    $mpdf = new mPDF();
    $mpdf->setDisplayMode("fullpage");
    $mpdf->WriteHTML("<h1 align='center'>Relatório de Clientes</h1> <hr/>");

            $html = "
            <table class='tabela'>
            <thread>
                <tr>
                    <th>Nome</span></th>
                    <th>Data de nascimento</span></th>
                    <th>Telefone</th>
                    <th>Endereço</th>
                </tr>
            </thread>
            <tbody>
            ";
                foreach($pessoas as $pessoa){

                    $html = $html ." <tr>
                        <td align='center'> {$pessoa["nome"]} </td>
                        <td align='center'> {$pessoa["data_formatada"]} </td>
                        <td align='center'> {$pessoa["telefone"]} </td>
                        <td align='center'> {$pessoa["endereco"]} </td>
                    </tr>";
                }

                $html = $html ."
                </tbody>
                </table>
                <h1>{$nome_filtrar}</h1>
                ";

    $css = file_get_contents('pdf_estilo.css');
    $mpdf->WriteHTML($css, 1);
    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit();

?>
  • yes the test variable was not sent via $_POST, because the form is sent to query clients.php (soon it will enter Else) and its function that fills the post is in registra_pessoa.php. correct me if I’m wrong. If so I’ll create a solution answer

  • Correct. I know it sounds ridiculous, but it would be like a form to send the data to two different pages?

  • You know ajax? da to do with it, when you write the name it sends this value to a page and returns this group. or you can do it without ajax but that’s the trick. Whatever you want?

  • sends an example with ajax?

  • ok Leticia, I just didn’t understand one thing. This form will it consult clients based on that name you write? if yes then why does Voce want to filter first? or else this filter will give you a list of names that contain what you wrote, and then check the clients for that list? Are you using jquery?

  • This form "filterClients" I made to filter a list of names that contains what to write in the "filtername" field, it sends to the same page "consult_clients.php", this page does the select: $group = filterPeople();

Show 1 more comment

1 answer

1


The variable $_POST['nomefiltrar']) is empty as nothing was sent via post to the page registra_pessoa.php.

What would be the solution

As your form sends a Submit to the same page you can use this code at the beginning of the page before the function call, to set the value of nomefiltrar:

$nome_filtrar = (isset($_POST['nomefiltrar'])) ? $_POST['nomefiltrar'] : "";

What does it do?

is an if written in more compact form, it checks whether it was sent a variable via post with the name nomefiltrar if yes, it assigns the value if it does not set an empty value ( '' );

And After?

After that you can implement in your function to receive this value as parameter:

$pessoas = filtraPessoas($nome_filtrar);

function filtraPessoas($nome){
    $grupo = '';
    $banco = abrirBanco();

    //lembrando que nome virá como o valor do input ou vazio
    $teste = $nome;

    $sql = "SELECT id, nome, DATE_FORMAT(nascimento, '%d/%m/%Y') AS data_formatada, endereco, telefone FROM pessoa WHERE nome LIKE '%$teste%' ";
    $resultado = $banco->query($sql);
    $banco->close();
    while($row = mysqli_fetch_array($resultado)){
        $grupo[] = $row;
    }
    return $grupo;
}
  • He didn’t even need the ajax, when he understood the situation, it was just changing some information to make it work. of course now you need to make the correct use of the value returned by the variable

  • It did not work. The "pdf_clients.php" page does not yet receive the value of the variable "name filter" to perform select in the report

  • but who does select is not the filterPeople() function that is inside registra_person.php? this page you said is new to me, is not in the question

  • Yes, the page registra_pessoa.php has the function select, the page pdf_clients.php uses a require on that page, but it cannot get the data from the "namegress" because the post only goes to the page "consult_clients.php"

  • Freedom is just doing the same thing for the other requires. If you don’t put what you have in your code it is impossible for us to give you a correct solution. is the tip for the next questions

  • Even if you put it on the other page, you cannot retrieve the value of the variable. If I put: $name_filter = (isset($_POST['nameguit'])) ? $_POST['filter name'] : "test"; and print this: <H1>{$filter_name}</H1> It returns "test". does not yet find the variable value. I will edit the post with the full page code

  • but Cvoce clicked on the button of the Ubmit, and the same went to the same page? ( takes out the Hidden from your form) does not have the why of it right?

  • Yes, it is Hidden because this button form only appears when you click on the "filter results" image

  • @Leticia good, this question has already been solved by my answer, now to stop generating this pdf will be easier to do via an ajax request. I suggest you open another question, asking about how to generate the pdf via ajax, passing the name variable. I’ll be happy to help you solve it, but the rest of your doubt is on another matter. end this nicely

Show 4 more comments

Browser other questions tagged

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