PHP only returns the the first value

时间:2018-06-04 17:03:34

标签: php jquery ajax

This has 3 rows and 3 buttons each rows. So, when I input a value at the first row and click it's button, it inserts the data. And then when I input at the second row and click its button, it inserts the value at the first row. And there's also something wrong with the button. I think it's because they have the same div class. Is there any way or solution for this? Ajax and jquery code:

    $(.btn).click(function() {                                    
          $getComment = $(".divComment").val();
          $getId= $(".inputId").val();

          $("#divHolder").load("insertData.php",{
              passComment: $getComment ,
              passId: $getId
          });
    });                                                                      

Php code:

     <?php foreach($query as $querySHOW) {?>                      

       <textarea class="divComment">COMMENT</textarea> //GET COMMENT
       <input type="text" class="inputId" value"<?php echo $querySHOW['id'];?>"> //GET ID                                                                              
       <input type="button" class="btn"> //BUTTON CLICK                                                                      

    <?php } ?> //SHOWS 3 ROWS with 3 BUTTONS EACH ROWS

1 个答案:

答案 0 :(得分:-1)

Try with this.

HTML

$(".btn").click(function() {    
      $getId = $(this).attr('id');                                
      $getComment = $("#divComment_"+$getId).val();

      $("#divHolder").load("insertData.php",{
          passComment: $getComment ,
          passId: $getId
      });
});  

PHP

<?php foreach($query as $querySHOW) {?>                      

   <div id="divHolder">                                         
      <textarea id="divComment_<?php echo $querySHOW['id'];?>">COMMENT</textarea> //GET COMMENT

      <input type="button" class="btn" id="<?php echo $querySHOW['id'];?>"> //BUTTON CLICK                                                                                                                                              
    </div>                                                             

 <?php } ?> //SHOWS 3 ROWS with 3 BUTTONS EACH ROWS