Help with Ajax Requests to get BD data

Asked

Viewed 86 times

1

I am currently using this Code below to search the page with the products related to the requested brand.

echo '<a class="marca" href="prod_index_marca.php?codmarca='.$res['codigo'].'">'.$res['nome_marca'].'</a>';

But I’m trying to create a Request so that this same content opens in a "DIV" without Refresh, so below:

echo '<a class="marca" style="cursor:pointer;">'.$res['nome_marca'].'</a>';

<script language="javascript">
    $(document).ready(function(){
    $('.marca').click(function(){
        $.ajax({url:"prod_index_marca.php?codmarca=<?php $res['codigo'];?>",success:function(data){
            $('#visual').html(data);
        }});
    });
});

And the page "prod_index_marca.php" is starting with the following PHP below, and with all the $res related to the products of the brand selected through its "codmarca":

<?php
include "conexao.php";

$codmarca = $_GET['codmarca'];
$sql = $pdo->prepare("SELECT * FROM produto WHERE codmarca = '$codmarca'");
$sql->execute();
foreach($sql->fetchAll() as $res){
?>

Once done, I verified that the page is being directed to the desired "DIV", but it is not bringing me the data of the products related to codmark requested.

If friends can give me a help to solve, or even indicate where I can research how to create this Request, I would be most grateful.

Thanks in advance for the attention to my problem.

2 answers

1

Problem solved with the help of Wees Smith.

Below working codes.

The Code below searches the page with the products related to the requested brand.

echo '<a class="marca" style="cursor:pointer;" id="'.$res['codigo'].'">'.$res['nome_marca'].'</a>';

The Request to open the content in the "DIV" without Refresh, was this way below:

<script language="javascript">
    $(document).ready(function(){
    $('.marca').click(function(){
        var cod = $(this).attr('id');
        $.ajax({url:"prod_index_marca.php?codmarca="+cod,success:function(data){
            $('#visual').html(data);
            }
        });
    });
});
</script>

And the page "prod_index_marca.php" that shows inside the "DIV" the product(s) referring to the selected brand was like this:

<?php
include "conexao.php";

    $codmarca = $_GET['codmarca'];
    $sql = $pdo->prepare("SELECT * FROM pagcabecalho, menu, produto WHERE codmarca = $codmarca");
    $sql->execute();
foreach($sql->fetchAll() as $res){
?>

<div id="prod" style="background-color:<?php echo $res['fundosite_cor']; ?>;width:33%; float:left; padding:10px 0;" class="center_prod_box">
    <div align="center" id="titulo" style="width:100%;">
        <?php echo $res['titulo']; ?>
    </div>
<div align="center" style="width:100%; height:130px; background-color:<?php echo $res['fundosite_cor']; ?>;">
    <div align="center">
        <a href="prod_detalhe_5.php?codigo=<?php echo $res['codigo']; ?>">
            <img style="width:100%; max-width:100px;" src="img_produtos/<?php echo $res['img01']; ?>" />
        </a>
    </div>
</div>
<div align="center" id="preco" style="width:100%;">
    <span style="">
        R$ <?php echo $res['preco']; ?>
    </span>
</div>                        
<div align="center" id="carrinho" style="width:100%;">
    <a href="prod_carrinho.php?acao=add&codigo=<?php echo $res['codigo']; ?>">
        <img style="width:100%; max-width:20px;" src="img/carrinho.png" title="Por no Carrinho" />
    </a>
</div>                        
</div>
<?php } ?>

Problem solved, and may be useful to friends in the future.

0


In his url:"prod_index_marca.php?codmarca=<?php $res['codigo'];?>" must be url:"prod_index_marca.php?codmarca=<?php echo $res['codigo'];?>", is missing function echo()php.

In the echo() tag <a> added the field id="" with the code of the mark:

echo '<a class="marca" style="cursor:pointer;" id="'.$res['codigo'].'">'.$res['nome_marca'].'</a>';
?>

and in the Jquery takes the id containing the code to pass through $.ajax():

<script language="javascript">
    $(document).ready(function(){
    $('.marca').click(function(){
        var cod = $(this).attr('id');
        $.ajax({
            url:"prod_index_marca.php?codmarca="+cod,
            dataType:"Json",
            success:function(data){
            $('#visual').html('');
            $.each(data,function(key,value){
                $('#visual').append(value);
            }
        }});
    });
});
  • 1

    Thanks Smith, that’s what was missing... Nothing better than someone from outside to see our mistakes, THANK YOU even...

  • Thank you for being able to help @Murilo

  • 1

    Smith, I came across another problem. I’m not getting to bring the products referring to the selected brand. Any brand I click, always brings me the same product. Could you see where I’m going wrong? Again THANK YOU...

  • I will edit my reply with a possible solution

  • edited, check if it worked

  • 1

    I do not use "id" and yes "code", I can change to: var cod = $(this).attr('codigo');

  • The id in question is an attribute of the tag only, which will count the value of the code

  • 1
Show 3 more comments

Browser other questions tagged

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