-1

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);
    } 
Gass
  • 7,536
  • 3
  • 37
  • 41
amit
  • 1
  • 1
  • 18
  • 28
  • 1) you're getting `.attr("ids")` but your html has `id=..` (not `ids`) on the buttons - not that that matters because 2) your `submit` event is on `.form-horizontls1` so `this` is the form, not the button 3) it's possible to submit a form without clicking a button. If you don't want the form submit (e.preventDefault) then cancel all form submits and handle button `click` – freedomn-m Sep 24 '21 at 08:09
  • Does this answer your question? [jQuery: how to get which button was clicked upon form submission?](https://stackoverflow.com/questions/5721724/jquery-how-to-get-which-button-was-clicked-upon-form-submission) – freedomn-m Sep 24 '21 at 08:11
  • Or: https://stackoverflow.com/questions/2066162/how-can-i-get-the-button-that-caused-the-submit-from-the-form-submit-event - `event.submittedVia` seems to be the solution there but can't see it on MDN (with a quick look) – freedomn-m Sep 24 '21 at 08:12
  • IDs must be unique within an HTML document. Your loop would still create `id="show1"`and `id="shows0"` multiple times, as soon as that loop iterates for more than one single time. – CBroe Sep 24 '21 at 08:28

1 Answers1

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);
    
Maksim Pukh
  • 1
  • 1
  • 1
  • 2