Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
Unable to get button value/attribute in jquery using php loop

I'm working on "like" and "dislike" module in jQuery with PHP,And I'm facing these two problems:

  1. Right now unable to get id of button (like=1, dislike=0)
  2. Query showing correct result but how to display ajax response under "like dislike" section ?

Here is my code, following code inside foreach loop:

<?php foreach // ?>
 <form class="form-horizontals1" method="post" >
                <input type="hidden"  id="ReviewId" name="ReviewId" value="<?php echo $rev->id;?>">
                
             <button class="likebutn_r" id="show<?php echo "1";?>"  type="submit"><img src="<?php echo base_url(); ?>/assets/img/thumb.png" height="24" width="24"></button>
             <label class="lilkcount">10(dynamic) </label>
            
            <button class="likebutn_r" id="shows<?php echo "0";?>"  type="submit"><img src="<?php echo base_url(); ?>/assets/img/thumbdown.png" height="24" width="24"></button>
             <label class="lilkcount">5(dynamic)</label>
        <div id="counter"></div>
        </form>
    <?php end foreach // ?> 
<script type="text/javascript">
    $(document).ready(function(){
        //likebutn_r
        $('.form-horizontals1').submit(function(e){
            
var ids = $(this).attr('ids');
 console.log(ids);          
        alert($(this).attr("id"));  
                e.preventDefault(); 
 $.ajax({
 url:'<?php echo base_url();?>main/AddVote',
 type:"post",
 data:new FormData(this),
 //dataType: 'json',
 processData:false,
 contentType:false,
 cache:false,
 async:false,
 success: function(data){
     console.log(data);
     alert(data);
    $('#counter').html(data); 
 }
 
 });
        });
    });
</script>
    

Here is my controller code, please tell me how I can get "like dislike" value in script and how I can show result in views ?

function AddVote()
    {
        $ReviewId=$_POST['ReviewId'];
        $vote=$_POST['vote'];
        echo $result['TotalUpVotes'] = $this->M_main->CountSubmittedCoinVote($ReviewId,$vote);
        echo $result['TotalDownVotes'] = $this->M_main->CountSubmittedDownVotes($ReviewId,$vote);
    } 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. First, move the form ouside the loop.

     <form class="form-horizontals1" method="post" >
     <?php foreach // ?>
     <div class="my-rev-container js-rev-container">
     ...
     </div>
     <?php end foreach // ?>
     </form>
    
  2. Add a class to the inputs, you whould access with

     <input type="hidden" class="js-form__rev" id="ReviewId" name="ReviewId" value="<?php echo $rev->id;?>">
    
  3. Finaly access them

     $('.form-horizontals1').submit(function(e){
     var ids = $(this).find(".js-form__rev");
     console.log(ids);
    
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda