0
Hello...
I’ve had a problem for many days and I can’t fix it.
The situation is as follows: I have a table that is created dynamically from the existing data in the database:
When the user hovers over an option, it is highlighted:
I need that when clicking on a selected row, I get the value corresponding to the column Name, and can for example put this value in a variable for future treatments.
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 of what is being returned from the database.
I need this variable in PHP, I will send its value to another page
Below is the code where all processes occur:
<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);
?>
But you want to put the value in a variable javascript is this ? Is that in your question I see almost no javascript and only see almost php.
– Isac
You can edit the question by clarifying whether you want the variable in javascript or php?
– Máttheus Spoo
The variable has to be in PHP, edited question, thank you.
– Carlos Eduardo Silva
Young, "dynamic" is when you change an element or several elements after the page is rendered, and this is done with Javascript. That is, your table is not dynamic because it comes ready from PHP.
– Sam
There is no way to save an element value to be used later in PHP. PHP runs on the server and the rendered page runs in the browser. What you can do is use Ajax and create a SESSION to save the value and then pull via PHP. Or save this value in a cookie via Javascript and then pull the JS itself.
– Sam
So if I understood well this code would serve for example only for a query in the database, how is one of its functions, a visualization of what is in the database, and for me to get the value of some line of this I would have to use Ajax creating a SESSION? I’m sorry because I’m a beginner in web language so my doubts seem so elementary.
– Carlos Eduardo Silva
@Carloseduardosilva You can pass a value to another page through a parameter in the URL.
– Valdeir Psr
@Valdeirpsr the question is how rsrs
– Carlos Eduardo Silva