2
The code below is for a page, which the user enters the name of a city, and checks if there are similar results. With this, it brings several results in a table created dynamically, depending on the data brought from the results, this is already working.
I need to make sure that by clicking on the row of the table corresponding to the desired result, the table is "cleaned" and stored the row data so that I can work with them, displaying other data related to the selected city in the table, but when I take the element by td it always brings me the first result regardless of which I click. Any idea that I can work to work?
<?php include_once 'conexao.php'
?>
<html>
<head>
<title>INDEX Foruns Regionais</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script text="text/javascript">
function mostraConteudo() {
esconde();
var nome = document.getElementById('td').innerHTML;
alert(nome);
}
function esconde() {
$('.table td').hide();
}
$(document).ready(function(){
$("p").click(function(){
$(this).fadeOut();
});
});
</script>
</head>
<body>
<h1>Pesquisa cidade</h1>
<form onclick="" name="formulario_busca" method="post"/>
<input type="text" name="nome_cidade"/>
<input type="submit" name="busca"/>
</form>
<?php
$busca = $_POST['nome_cidade'];
$query = "SELECT * FROM cidades WHERE cidade LIKE '%".$busca."%'";
$resultado = mysqli_query($conexao, $query);
mysqli_fetch_array($resultado,$lista_Cidades);
?>
<table class="table table-striped table-bordered">
<?php
if ((mysqli_num_rows($resultado)>0) && ($busca != "") ):
while ($linha = mysqli_fetch_assoc($resultado)) {
?>
<tr>
<td id="td" onclick="mostraConteudo()"><?= $linha['cidade']?></td>
</tr>
<?php
} echo "<br/>";
endif; if(mysqli_num_rows($resultado)<=0):
echo "Cidade não encontrada";
endif;
?>
</table>
</body>
</html>
I think that you have to pass the element itself clicked on the function. Example:
<td id="td" onclick="mostraConteudo(this)"><?= $linha['cidade']?></td>
– Leandro Lima
Why don’t you use Html5 data-attrs instead of taking the html value?
– AnthraxisBR