Colbyd
Colbyd

Reputation: 371

How do I get jQuery/AJAX result into table cell

I have this script here that outputs a result from a sql db query but I can not figure out how to get it to put the result in the table cell specified.

Here is query:

<script type="text/javascript">

$(document).ready(function(){
$('.typeval').change(function(){
    var movement =  $(this).val();
    var client_id = $(this).parent().siblings().find('.clientval').val();
    var class_id = <? echo $class_id; ?>;

$.ajax({ 
  type: "POST",
  url: "movement_count.php",
  data: {movement:movement, client_id:client_id, class_id:class_id},
  dataType: "json", 
  success:(function(output) {

      $.each(output, function(index, value){
      alert(value);
      $(".count").append(output[value]);
      }) // each

   }) // success

  }); // ajax

  }); // .typeval

});  // document

</script>   

I want to put the result of this (value):

alert(value);

into here:

<td><label class="count"></label></td>

The <td> I am targeting on the same row as the <select> menu that is triggering the result. There will also be multiple table rows with this exact same cell <td>. So I need to target the <td> on this row only somehow. Can someone help me with this? Im not sure if I need the $.each but my php query is a mysql_fetch_row array even though the value returned will always be just one number.

Here is the HTML markup for the table cell I need the value in:

<td><label class="count"></label></td>

JS source code for class=count:

$(".count").append(output[index]);

ITS WORKING!!!! here is the code below

$(document).ready(function(){
$('.typeval').change(function(){
    var movement    =  $(this).val();
    var client_id   = $(this).parent().siblings().find('.clientval').val();
    var class_id    = <? echo $class_id; ?>;
        $count      = $(this).parents('tr').find('label.count');

$.ajax({ 
  type: "POST",
  url: "movement_count.php",
  data: {movement:movement, client_id:client_id, class_id:class_id},
  dataType: "json", 
  success:(function(output) {

      $.each(output, function(index, value){
      //alert(value);
      $count.append(output[index]);
      }) // each

   }) // success

}); // ajax

}); // .typeval

});  // document

Upvotes: 0

Views: 1965

Answers (2)

Colbyd
Colbyd

Reputation: 371

Working code:

$(document).ready(function(){
   $('.typeval').change(function(){
    var movement    =  $(this).val();
    var client_id   = $(this).parent().siblings().find('.clientval').val();
    var class_id    = <? echo $class_id; ?>;
        $count      = $(this).parents('tr').find('label.count');

 $.ajax({ 
  type: "POST",
  url: "movement_count.php",
  data: {movement:movement, client_id:client_id, class_id:class_id},
  dataType: "json", 
  success:(function(output) {

      $.each(output, function(index, value){
      //alert(value);
      $count.append(output[index]);
      }) // each

   }) // success

}); // ajax

}); // .typeval

});  // document

Upvotes: 0

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14292

Replace this

$("label.count<?php echo $client_id; ?>").append(output[value]);

With

$("label.count<?php echo $client_id; ?>").append(value);

Or alternately with this

$("label.count<?php echo $client_id; ?>").append(output[index]);

Hope this will help !!

Upvotes: 1

Related Questions