lundi 8 mai 2017

JQuery returning only attribute of first row on click function

I am populating a list of rows from my database which has a checkbox for each user(data-uid). I'm using the data-uid with an Ajax call to my PHP file which will make a listing public when checked and private when unchecked. My code seems good but when I check on any checkbox of any row, only the first row is affected. I did a trace and I noticed that for whichever checkbox I click, it's getting the value of only the VERY first checkbox.

Here's my code. Any help is appreciated:

PHP Snippet

<?php
    foreach($results as $row){
?>
        <div class="switch">
            <?php
                if($row['is_active'] == 1){
            ?>
                <input data-uid="<?= $row['listing_uid'] ?>" data-active="1" id="cmn-toggle-1" class="switchBtn cmn-toggle cmn-toggle-round" type="checkbox" checked />
            <?php
                }else if($row['is_active'] == 0){
            ?>
                <input data-uid="<?= $row['listing_uid'] ?>" data-active="0" id="cmn-toggle-1" class="switchBtn cmn-toggle cmn-toggle-round" type="checkbox" />
            <?php
                }
            ?>
            <label for="cmn-toggle-1"></label>
        </div>
<?php
    }
?>

JQuery Code

$(".switchBtn").click(function(e){
    e.preventDefault();
    var data_uid = $(this).attr('data-uid');
    var data_active = $(this).attr('data-active');
    var data_string = 'data_uid='+data_uid+'&data_active='+data_active;
    alert(data_string);
    $.ajax({
        url: '../includes/set_listing.php',
        type: 'POST',
        data: data_string,
        success:function(e){
            window.location.reload();
        },
        error:function(e){
            //alert(e);
        }
    }); 
});




Aucun commentaire:

Enregistrer un commentaire