Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!