Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

62
Visualizações
How to get ajax response using custom attribute in php loop

I am working with ajax with php,I have following buttons in loop,I just want to show/fetch data(number) on correct place/div using "attr" in jquery but not working in my side

Here is my button in loop

<?php
foreach //

 <button class="btn likebutn_r" data-datac="1" data-datacw="<?php echo $WalletAddress; ?>" data-datacr="<?php echo $rev->id; ?>" data-datacoin="<?php echo $rev->coin_id; ?>" data-datasymbol="<?php echo $rev->symbol; ?>" id="show<?php echo "1";?>"  value="1" type="submit"><img src="<?php echo base_url(); ?>/assets/img/thumb.png" height="24" width="24"></button>
 
 <div id="<?php echo $rev->id; ?>">12(dynamic)</div>
endforeach//

Here is my ajax code,How can i get data using custom attribute (via pass ReviewId)? Thanks in advance

<script type="text/javascript">
    $(document).ready(function () {
        $('.btn').click(function (e) {
            var vote = $(this).data('datac');
            var review = $(this).data('datacr');
            var CoinId = $(this).data('datacoin');
            var symbol = $(this).data('datasymbol');
            var WalletAddress = $(this).data('datacw');

            var datacrp = $(this).data('datacrp');
            $('#' + review).hide();
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: '<?php echo base_url();?>main/AddVote',
                data: {
                    vote: vote,
                    review: review,
                    CoinId: CoinId,
                    symbol: symbol,
                    WalletAddress: WalletAddress
                },
                success: function (data) {
                    alert(data);
                    $('#' + review).html(data);
                }
            });

        });
    });
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hope this code help you. *Reminder the js code below have to implement inside the php file so the Post url ajax will get the value.

$('body').on('click', '.btn', function(e){ 
  var allData = {
    "vote": $(this).data('datac'),
    "review": $(this).data('datacr'),
    "CoinId": $(this).data('datacoin'),
    "symbol": $(this).data('datasymbol'),
    "WalletAddress": $(this).data('walletaddress'),
  }
  e.preventDefault();

  $.ajax({
      type: 'POST',
      url:'test.com',
      data: allData,
      success: function(data) {
            $(this).attr('data-datac', data['vote']);
            $(this).attr('data-datacr', data['review']);
            $(this).attr('datacoin', data['CoinId']);
            $(this).attr('data-datasymbol', data['review']);
            $(this).attr('data-datacoin', data['review']);
            $(this).attr('data-datasymbol', data['symbol']);
            $(this).attr('data-walletaddress', data['WalletAddress']);
      }
  });
});
<button 
class="btn likebutn_r" 
data-datac="1" 
data-datacw="teste"
data-datacr="asdfasf" 
data-datacoin="asdfasdf"
data-datasymbol="asdfasdf"
data-datacrp="asfsa"
id="31"
data-walletaddress="dsaf"
value="1" 
type="submit">Hello</button>

<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda