Request via GET

Asked

Viewed 351 times

1

I am trying to create a search system using a Request to get the search result.

I am not able to bring the product data related to the title researched.

Whatever the searched word, it always brings me the last item of the product table.

Code for search or search 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 in Madrid, but I think the problem lies in this 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 fetch the product according to the research.

If your friends can help me with that question, I’d be grateful.

2 answers

0

Try it this way:

$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>
    ';
}

The way you were doing, the array $resultados would show only the last one anyway, but now, you separate the array within a loop while() separating the values associatively with the function fetch() and the parameter PDO::FETCH_ASSOC.

  • Hello Wees Smith, I made the change you recommended, but without success. I decided to do with FORM and it worked in part. It brings me the data searched correctly, but not using the SCRIPT, but the page that is in the FORM ACTION <form action="prod_index_consulta.php" method="get">. I edited the Code for research, with this you can show me how to increment or open it in DIV #visual using SCRIPT?

0


I am posting the solution I found to bring only the data related to the consultation made by the title of the products.

The Code for search or search was 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 the product data, searched by the title:

<?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 those who need it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.