Taking a TD value from an HTML table to feed a PHP variable

Asked

Viewed 260 times

-1

I’m developing a web system. In this system I have tables with many columns since they are a lot of information, to reduce this I selected the most important information to be displayed and put in this table, but if the user needs to consult ALL the information, I need to have this option for him, for this I wanted to take the code that is in a td of this table to use as a restriction parameter at the time to select which will display all the information of a particular employee for example.

Table Code

                     
                         $result_funcionario =  "SELECT * FROM tb_funcionarios";
                         $resultado_funcionario = mysqli_query($con, $result_funcionario);
                    

                    ?>


                
                     
                

                    <table cellpadding="0" cellspacing="0" border="0" class="table table-bordered"  id="hidden-table-info">
                    <thead >
                      <tr>
                        <th> </th>
                        <th >Código</th>
                        <th class="">Nome</th>
                        <th class="-">CPF</th>
                        <th class="">RG</th>
                        <th class="">Celular</th>   
                        <th class="">Salário</th>                       
                        <th class="">Situação</th>                      
                      </tr>
                    </thead>
                    <tbody>
                 
                 
                     <?php while ($row_funcionario = mysqli_fetch_assoc($resultado_funcionario)) { ?>
          
                 
               
                      <tr class="">
                        <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
                        <td class="" name="codigo_func"><?php echo $row_funcionario ["codigo_funcionario"];   ?></td>
                        <td class="" name="nome_func"><?php echo $row_funcionario ["nome_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["cpf_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["rg_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["celular_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["salario_funcionario"];   ?></td>                       
                        <td class=""><?php echo $row_funcionario ["situacao_funcionario"];   ?></td>
                      </tr>

              


                        <?php   } ?>

In this case, I need to take the value contained within the td function_name so that on the textex.php page I can display the values only about the employee that I capture the id

  • will need to use jquery

  • If there is an answer that solved your problem mark it as accepted, see https://i.stack.Imgur.com/evLUR.png

2 answers

0

depending on the situation of to pass via get.

                     <?php while ($row_funcionario = mysqli_fetch_assoc($resultado_funcionario)) { 
                        $nome = $row_funcionario ["nome_funcionario"]; ?>
                         
                        
                 
               
                      <tr class="">
                        <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
                        <td class="" name="codigo_func"><?php echo $row_funcionario ["codigo_funcionario"];   ?></td>
                        <td class="" name="nome_func"> <a href="textex.php?nome=<?=$nome?>"></a></td>
                        <td class=""><?php echo $row_funcionario ["cpf_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["rg_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["celular_funcionario"];   ?></td>
                        <td class=""><?php echo $row_funcionario ["salario_funcionario"];   ?></td>                       
                        <td class=""><?php echo $row_funcionario ["situacao_funcionario"];   ?></td>
                      </tr>

              


                        <?php   } ?>

  • Could exemplify me the way I can use the GET Samuel, I’m breaking my head and I can not fit. I thank you for the attention

  • youtube.com/watch? v=6cIBQGV8-mi in this video you will find a little sovre get, how to pass the value and how to catch it from the url. It would be bob who delves into the GET and POST methods, will need a lot of them to program.

0

You will need Jquery

Comments on the code

  1. Sending by a form via POST

    I put to pick up the amount contained within the td codigo_func for finding more correct because there can be equal names. But if you want to take the value contained within the td nome_func make the changes:

    • .find('td.codigo_func') por .find('td.nome_func')

    • <td class="" name="nome_func"> por <td class="nome_func" name="nome_func">

    $(document).ready(function ()
    {   
      //$('#hidden-table-info').find('td.codigo_func') vai encontrar todas as tds cuja classe é codigo_func
      $('#hidden-table-info').find('td.codigo_func').each(function ()
      {
        $(this).click(function ()
        {
           var tdValue = $(this).html(); //texto da td clicada
           // o input que vai receber essa variável como value
           document.getElementById("codFun").value = tdValue;
           
        });
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <table cellpadding="0" cellspacing="0" border="0" class="table table-bordered"  id="hidden-table-info">
        <thead>
        <tr>
            <th> </th>
            <th >Codigo</th>
            <th class="">Nome</th>
            <th class="-">CPF</th>
            <th class="">Email</th>
            <th class="">Celular</th>                       
        </tr>
        </thead>
        <tbody>
                        
        <!-- ## Exemplo gerado pelo while do PHP ## -->               
                         
                       
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">123</td>
            <td class="" name="nome_func">Leo</td>
            <td class="">111.222.333-01</td>
            <td class="">[email protected]</td>
            <td class="">23344559</td>
        </tr>
               
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">124</td>
            <td class="" name="nome_func">Alicia</td>
            <td class="">000.000.002-02</td>
            <td class="">[email protected]</td>
            <td class="">22433443</td>
        </tr>
               
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">125</td>
            <td class="" name="nome_func">Samuel</td>
            <td class="">222.000.444-03</td>
            <td class="">[email protected]</td>
            <td class="">45678998</td>
        </tr>
               
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">128</td>
            <td class="" name="nome_func">Ciclano</td>
            <td class="">903.904.905-06</td>
            <td class="">[email protected]</td>
            <td class="">76543787</td>
        </tr>
               
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">129</td>
            <td class="" name="nome_func">Athena</td>
            <td class="">456.765.345-00</td>
            <td class="">[email protected]</td>
            <td class="">5678907</td>
        </tr>
                                       
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">130</td>
            <td class="" name="nome_func">Maria</td>
            <td class="">325.627.423-87</td>
            <td class="">[email protected]</td>
            <td class="">7654321</td>
        </tr>
           
        <tr class="">
            <td class=""><a href="testex.php"><img src="lib/advanced-datatable/images/details_open.png"></a></td>
            <td class="codigo_func" name="codigo_func">131</td>
            <td class="" name="nome_func">Fernanda</td>
            <td class="">567.765.567-76</td>
            <td class="">[email protected]</td>
            <td class="">8765438</td>
        </tr>

    </table>

    <!-- ##### Supondo que será enviado por um form via POST #### -->
    
    <!-- ## Elemento que vai receber como value a variável do JavaScript ## -->  
  
    <input type="txt" name="" id="codFun" value="">
    

  1. Via GET - code modifications

    Script

        $(document).ready(function ()
        {   
    
          $('#hidden-table-info').find('a.codigo_func').each(function ()
          {
            $(this).click(function ()
            {
               var aValue = $(this).html();
               $(".codigo_func").attr("href", "testex.php?codFun="+aValue);
    
            });
          });
        });
    

    HTML

    <tr class="">
        <td class=""><a href=""><img src="lib/advanced-datatable/images/details_open.png"></a></td>
        <td class="" name="codigo_func"><a href="" class="codigo_func"><?php echo $row_funcionario ["id"];   ?></a></td>
        <td class="" name="nome_func"><?php echo $row_funcionario ["nome"];   ?></td>
        <td class=""><?php echo $row_funcionario ["cpf"];   ?></td>
        <td class=""><?php echo $row_funcionario ["email"];   ?></td>
        <td class=""><?php echo $row_funcionario ["telefone"];   ?></td>
    </tr>
    
  • The methodology of get I understood and I managed to put into practice, but for me the POST is more interesting, however I could not understand to put into practice, can you explain to me how the input will receive the value clicked? where will the click be? Thank you from now.

  • $(this). click(Function() is a function on the clicked td and this function will create a variable whose value is the text of the clicked td.

  • In the example I put above (POST) the click is in the codes ( 123 - 124 -125 etc.. ) But if you adapt the code for the names, then you will have to click on the names.

  • The value of the input will be sent via post to the PHP page as long as it is included in a form.

Browser other questions tagged

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