Get the tr value that the user clicked on a PivotTable

0

Hello ...

I have a problem for many days and I can not resolve it.

The situation is as follows: I have a table that is created dynamically from the data in the database:

Whentheuserhoveroveranoption,itishighlighted:

Ineedtoclickonaselectedrow,IgetthevaluecorrespondingtotheNamecolumn,andcanforexampleputthisvalueintoavariableforfuturetreatments.

ThebigproblemisthatIdonotunderstandawaytodothisinatablethatisdynamic,thatis,itscontentsvaryaccordingtothedatathatisbeingreturnedfromthedatabase.

INEEDTHATVARIABLEINPHP,BECAUSEIWILLSENDTHEVALUEOFITTOANOTHERPAGE

Hereisthecodewhereallprocessesoccur:

<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="img/bus-coor.png">
    <link rel="stylesheet" href="/css/style.css">
    <title>Buscar Coordenador</title>
</head>
<body>
<form class="registro form" method="get">
    <fieldset><legend>BUSCAR COORDENADOR</legend>
        <label class="labels" for="cNomCoo">Nome </label>
        <input type="text" name="tNomCoo" id="cNomCoo">
        <br>
        <input type="submit" onclick="criarVariavel();" value="Buscar">
    </fieldset>
</form>
<script>
    function criarVariavel() {
        <?php
        $nome_coo = $_GET['tNomCoo'];
        ?>
    }
</script>
</body>
</html>

<?php
include ('configBD.php');

if(!empty($nome_coo)){ // se a varivel tiver valor
// cria a instrução SQL que vai selecionar os dados
    $query = ("SELECT idCoo, tNomCoo, tEma, tTel, tFun FROM coordenador WHERE tNomCoo LIKE '%".$nome_coo."%'");
// executa a query
    $dados = mysqli_query($conexao, $query) or die(mysqli_error());
// transforma os dados em um array
    $linha = mysqli_fetch_assoc($dados);
// calcula quantos dados retornaram
    $total = mysqli_num_rows($dados);
}
else { // se a varivel não tiver valor seleciona retorna todos os dados
// cria a instrução SQL que vai selecionar os dados
    $query = sprintf("SELECT * FROM coordenador ");
// executa a query
    $dados = mysqli_query($conexao, $query) or die(mysqli_error());
// transforma os dados em um array
    $linha = mysqli_fetch_assoc($dados);
// calcula quantos dados retornaram
    $total = mysqli_num_rows($dados);
}
?>

<html>
<head>
    <meta charset="UTF-8">
    <title>Resultado da Pesquisa</title>
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
<table style="font-weight: bolder; text-align: center">
        <td style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 2px;">ID</td>
        <td style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 2px; width: 30%; overflow: auto;">Nome</td>
        <td style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 2px; width: 30%; overflow: auto;">E-mail</td>
        <td style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 2px; width: 20%; overflow: auto;">Telefone</td>
        <td style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 2px; width: 30%; overflow: auto;">Categoria Funcional</td>
    </tr>
</table>
<?php
// se o número de resultados for maior que zero, mostra os dados
if($total > 0) {
    // inicia o loop que vai mostrar todos os dados
    do {
        ?>
        <table style="border-spacing: 0px; text-align: center;">
            <tr style="cursor: pointer; border-radius: 3px;" onclick="selecionaLinha" onMouseOver="javascript:this.style.backgroundColor='#75ee83'" onMouseOut="javascript:this.style.backgroundColor=''">
                <td id="cNomCoo" class="celula" style="border-bottom: 1px solid #5e5e5e; alignment: center; padding: 7px;"><?=$linha['idCoo']?></td>
                <td class="celula" style="border-bottom: 1px solid #5e5e5e; padding: 2px; width: 30%; overflow: auto;"><?=$linha['tNomCoo']?></td>
                <td class="celula" style="border-bottom: 1px solid #5e5e5e; padding: 2px; width: 30%; overflow: auto;"><?=$linha['tEma']?></td>
                <td class="celula" style="border-bottom: 1px solid #5e5e5e; padding: 2px; width: 20%; overflow: auto;"><?=$linha['tTel']?></td>
                <td class="celula" style="border-bottom: 1px solid #5e5e5e; padding: 2px; width: 20%; overflow: auto;"><?=$linha['tFun']?>
            </tr>
            <br>
        </table>
        <?php
        // finaliza o loop que vai mostrar os dados
    }while($linha = mysqli_fetch_assoc($dados));
}// fim do if
?>
</body>
</html>
<?php
// tira o resultado da busca da memória
mysqli_free_result($dados);
?>
    
asked by anonymous 12.12.2018 / 04:42

1 answer

0
  

"I need that when I click on a selected line, I get the value   corresponding to the Name column, and can for example put this value   in a variable for future treatments. "

Taking the value of idCoo and taking a $ result variable on the server side of php to run at the time of the click, and let's also read a variable that can be reused later / p>

  

Let's split into two files, the first one (which you're working on)   and the second is the php where you will save the variable with the content.

     

In the first file:

Before the table code, put this:

<script>
function pegacampo_ajax($val) {
 var dado = $('#idCooDiv'+$val).html(); //pega o texto do div pelo id
 var link = "pegacampo_ajax.php"; //endereco do segundo arquivo php
 $.ajax({type: 'POST', url: link, data: dado, processData: false, contentType: false }).done(function (result) {
 $('#resultado').html(result);
 $('#resultado').css('display', 'block');
});
  return false;
}
</script>

Edit this part in your table:

<td id="cNomCoo">
 <div style="height:50px;width:100px;color:red;font-weight:600;" onclick="pegacampo_ajax('idCooDiv<?=$linha['idCoo']?>')" id="idCooDiv<?=$linha['idCoo']?>">
  <?=$linha['idCoo']?>
 </div>
</td>

Show result (put below table):

<div id="resultado" style="color:red;font-weight:600;width:100px;height:50px;"></div>

In the second file, named pegacampo_ajax.php

In it you write:

<?php
function pegacampo_ajax(){
if (isset($_POST['dado'])){
$resultado = "$_POST['dado']";

//nesta linha tu pode salvar o dado numa session do php
//vou deixar em branco para voce fazer

}
else{
$resultado = "Não há conteúdo";
}
return $resultado;
}

$resultado = pegacampo_ajax();
echo $resultado;

?>
  

The big problem is that I do not understand a way to do this in a   table that is dynamic, that is, its content varies according to the   data that is being returned from the database

If you do not understand it then ask someone to edit my answer to you with the final code, but this is instead of getting the name, get the id.

I did not execute, may have error

To change is just to change the text that is inside the div and the value will change, can use for the name, or whatever you want

The cat leap is concatenating the id that comes from the database with the id of the div to be unique

I concatenated the id that comes from the database along with the id of the div, making it unique, and then, when clicking, takes its value via ajax, without repeating, because the name is id="name + id", the id always different.

    
29.12.2018 / 06:11