views:

124

answers:

4

Hi

I am working to get each value of the checked element and post them to php. But it only gets first value of just one checked item.

here is

$("#conf").click(function(){
    var count = $("input:checked").length;
    for(i = 0; i < count; i++) {
         a = $("input:checked").val();
     $.post("reqs.php?act=confirm", { ID: a }, function(data) { });
     $('#'+a).parents(".req")
         .animate({ backgroundColor: "#fbc7c7" }, "fast")
         .animate({ opacity: "hide" }, "slow");
    }
});

And HTML

<?php while ($info = mysql_fetch_assoc($result)) { ?>
<tr class="req">
    <td style="width: 29px">
        <input name="confirm" type="checkbox" id="<?php echo $info['ID']; ?>" value="<?php echo $info['ID']; ?>" />
    </td>
    <td style="width: 70px" class="style5"><?php echo $info['email']; ?></td>
    <td style="width: 72px" class="style5"><?php echo $info['name']; ?></td>
    <td style="width: 88px" class="style5"><?php echo $info['username']; ?></td>
    <td style="width: 76px" class="style5"><?php echo $info['country']; ?></td>
    <td style="width: 76px" class="style5"><?php echo $info['bus']; ?></td>
    <td style="width: 67px" class="style5"><?php echo $info['website']; ?></td>
    <td style="width: 97px" class="style5"><?php echo $info['music']; ?></td>
    <td style="width: 78px" class="style5"><?php echo $info['radio']; ?></td>
</tr>
<?php } ?>

How can i get value of checked items in that for loop ?

Thanks

A: 
$("input:checked").each(function() {
   alert($(this).val());
});
cballou
+1  A: 

You should iterate over all the checked elements, you can use $.each, then add the results to an object and post that object to the server:

$("#conf").click(function(){
  var data = {};
  $('input:checked').each(function () {
    var el = $(this);
    data[el.attr('id')] = el.val();
  });

  $.post("reqs.php?act=confirm", data, function(response) { });
});

The data object will be formed by Key/Value pairs containing the ID/Value of the checked elements.

CMS
Should be using the `name` attribute instead of ID. Other than that, good answer.
Josh Stodola
@Josh, yes agree, I used id attribute because it seems that the OP assigns a different value that he is willing to use. `id="<?php echo $info['ID']; ?>"`
CMS
it does not work, i cant understand, i changed to "name" attr as you wrote but no luck
Ahmet vardar
@Ahmet: I was trying to make only **one** Ajax request sending all the data, not making a request per checkbox.
CMS
oh sorry, i am new in Jquery, thx
Ahmet vardar
A: 

Even better, let the magic of jQuery work for you:

$("#conf").click(function(){
  $("input:checked").each(){
    var a = $(this).val();
    $.post("reqs.php?act=confirm", { ID: a }, function(data) { });
    $('#'+a).parents(".req")
      .animate({ backgroundColor: "#fbc7c7" }, "fast")
      .animate({ opacity: "hide" }, "slow");
  });
});
Sixten Otto
A: 

Change

a = $("input:checked").val();

to

a = $("input:checked").eq(i).val();

This will get you the value for each subsequent item.

And use Firebug! :)

Pawel Krakowiak
thx, that is what i was looking for !
Ahmet vardar