Request via GET

1

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.

    
asked by anonymous 10.06.2018 / 23:43

2 answers

0

I'm posting the solution I found to bring only the data regarding the query made by the title of the products.

The Code for Search or Search looks like this:

<div>
   <span>Busca Avançada</span>    
   <input type="image" id="buscar" img src="img/search.gif" />    
   <input type="text" id="palavra" placeholder="Buscar por..."/>
</div>

<script>
function buscar(palavra){
    var page = "prod_index_consulta_5_img.php";
    $.ajax
            ({
                type:'GET',
                dataType: 'html',
                url: page,
                beforeSend: function(){
                    $("#visual").html("Carregando...");
                },
                data: {palavra: palavra},
                success: function (msg)
                {
                    $("#visual").html(msg);
                }
            });
    }
    $('#buscar').click(function (){
        buscar($("#palavra").val())
});
</script>

The code to receive product data, searched for by the title looks like this:

<?php
    include "conexao.php";
    $palavra = $_GET['palavra']; 
    $sql = $pdo->prepare("SELECT * FROM produto WHERE titulo LIKE '%".$palavra."%'");
    $sql->execute();
    foreach($sql->fetchAll() as $res){

    echo'
    <div id="prod">
        <div align="center" id="titulo">
            '.$res["titulo"].'
        </div>
        <div align="center">
            <div align="center">
                <a href="prod_detalhe_5.php?codigo='.$res["codigo"].'">
                    <img src="img_produtos/'.$res["img01"].'" />
                </a>
            </div>
        </div>
        <div align="center" id="preco">
            <span>R$ '.$res["preco"].'</span>
        </div>                        
        <div align="center" id="carrinho">
            <a href="prod_carrinho.php?acao=add&codigo='.$res["codigo"].'">
                <img src="img/carrinho.png" title="Por no Carrinho" />
            </a>
        </div>                        
     </div>
    ';
    }
?>    

I hope it will be useful to anyone who needs it.

    
28.06.2018 / 23:17
0

Try this:

$sql = "SELECT * FROM pagcabecalho, menu, produto WHERE titulo LIKE :pesquisa";
$resultados = $pdo->prepare($sql);
$resultados->bindParam(':pesquisa', $buscar, PDO::PARAM_STR);
$resultados->execute();
while($res=$resultados->fetch(PDO::FETCH_ASSOC)){
    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>
    ';
}

As you were doing, the $resultados array would only show the last one, but now, you separate the array inside a while() loop by separating the results associatively with the fetch() function and the parameter PDO::FETCH_ASSOC .

    
11.06.2018 / 01:56