I'm trying to create a search system using a Request to get the search result.
I am not able to bring the product data for the searched title.
Whatever the searched word, it always brings me the last item in the product table.
Search or search code below:
<form action="prod_index_consulta.php" method="get">
<span>Busca Avançada</span>
<input type="image" name="busca" img src="img/search.gif" />
<input type="text" value="" name="buscar"/>
</form>
<script language="javascript">
$(document).ready(function(){
$('.search').click(function(){
var cod = $(this).attr('id');
$.ajax({url:"prod_index_consulta.php?buscar="+cod,success:function(data){
$('#visual').html(data);
}
});
});
});
</script>
Below code to receive product data, searched by title:
<?php
include "conexao.php";
$buscar = $_GET['buscar'];
$sql = "SELECT * FROM pagcabecalho, menu, produto WHERE titulo LIKE :pesquisa";
$resultados = $pdo->prepare($sql);
$resultados->bindParam(':pesquisa', $buscar, PDO::PARAM_STR);
$resultados->execute();
foreach($resultados as $res){
echo'
<div id="prod" style="background-color:'.$res["fundosite_cor"].';width:33%; float:left; padding:10px 0;" class="center_prod_box">
<div align="center" id="titulo" style="width:100%;">
'.$res["titulo"].'
</div>
<div align="center" style="width:100%; height:130px; background-color:'.$res["fundosite_cor"].';">
<div align="center">
<a href="prod_detalhe_5.php?codigo='.$res["codigo"].'">
<img style="width:100%; max-width:100px;" src="img_produtos/'.$res["img01"].'" />
</a>
</div>
</div>
<div align="center" id="preco" style="width:100%;">
<span style="">R$ '.$res["preco"].'</span>
</div>
<div align="center" id="carrinho" style="width:100%;">
<a href="prod_carrinho.php?acao=add&codigo='.$res["codigo"].'">
<img style="width:100%; max-width:20px;" src="img/carrinho.png" title="Por no Carrinho" />
</a>
</div>
</div>
';
}?>
I'm a layman on the subject and I'm trying to learn a little bit about it, but I think the problem lies in that line below the script:
$.ajax({url:"prod_index_consulta.php?buscar="+cod,success:function(data){
Because it gives me the impression that something is missing for him to get the product according to the search.
If friends can help me with this, I'll be grateful.