jQuery/PHP - detect which button is pressed -
i'm making app (admin page), loaded users in <table>
, have option activate , deactivate each user. problem don't know how detect 'activate' or 'deactivate' button pressed, code works first user in list. code:
adminpage.php:
<?php $getdata = $mysqli->query("select * login"); while($row = $getdata->fetch_assoc()): ?> <tr id="datarows"> <td id="firsttd"><?php echo $row['user_id']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['email']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['active']; ?></td> <?php if($row['active'] == 1){ ?> <td><label id="userid" hidden><?php echo $row['user_id']; ?></label> <label id="useractive" hidden><?php echo $row['active']; ?></label> <label id="optionlabel"><?php echo 'deactivate'; ?></label></td> <?php } else{ ?> <td><label id="userid" hidden><?php echo $row['user_id']; ?></label> <label id="useractive" hidden><?php echo $row['active']; ?></label> <label id="optionlabel"><?php echo 'activate'; ?></label></td> <?php } endwhile; ?>
script.js:
$("#optionlabel").click(function(){ $.post("option.php", {"id" : $("#userid").html(), "com" : $("#useractive").html()}, function(data){ if(data == "updated"){ window.location.href = "adminpage.php"; } } ); });
try this
//adminpage.php
$getdata = $mysqli->query("select * login"); while($row = $getdata->fetch_assoc()): ?> <tr id="datarows"> <td id="firsttd"><?php echo $row['user_id']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['email']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['active']; ?></td> <?php if($row['active'] == 1){ ?> <td><label class="userid" hidden><?php echo $row['user_id']; ?></label> <label class="useractive" hidden><?php echo $row['active']; ?></label> <label class="optionlabel"><?php echo 'deactivate'; ?></label></td> <?php } else{ ?> <td><label class="userid" hidden><?php echo $row['user_id']; ?></label> <label class="useractive" hidden><?php echo $row['active']; ?></label> <label class="optionlabel"><?php echo 'activate'; ?></label></td> <?php } endwhile; ?>
//script.js
$(".optionlabel").click(function(){ $.post("option.php", {"id" : $(this).siblings('.userid').html(), "com" : $(this).siblings('.useractive').html()}, function(data){ if(data == "updated"){ window.location.href = "adminpage.php"; } } ); });
Comments
Post a Comment