Help with Ajax requests to retrieve DB data

1

I am currently using this Code below to search for the page with the products for the requested tag.

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

But I'm trying to create a Request for this same content to open in a "DIV" without Refresh, like this:

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 $ res referring 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){
?>

asked by anonymous 04.05.2018 / 12:20

2 answers

0

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

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

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

and in Jquery takes the id containing the code to pass $.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);
            }
        }});
    });
});
    
04.05.2018 / 12:49
1
___ erkimt ___ Help with Ajax requests to retrieve DB data ______ qstntxt ___

I am currently using this Code below to search for the page with the products for the requested tag.

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

But I'm trying to create a Request for this same content to open in a "DIV" without Refresh, like this:

<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" is starting with the following PHP below, and with all $ res referring to the products of the brand selected through its "codmarca":

<?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 } ?>

04.05.2018 / 19:46