Reputation: 917
I'm making some app (admin page), I loaded all users in <table>
, also I have option to activate and deactivate each user. Now my problem is I don't know how to detect which 'activate' or 'deactivate' button is pressed, my code only works for first user in list. This is my code:
adminPage.php:
<?php
$getData = $mysqli->query("select * from 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";
}
}
);
});
Upvotes: 0
Views: 121
Reputation: 2301
this code work better use this:
//adminPage.php
<?php
$getData = $mysqli->query("select * from 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="optionLabel" onclick="updatestatus(<?php echo $row['user_id']; ?>, 1);">Deactivate</label></td>
<?php } else{ ?>
<td>
<label id="optionLabel" onclick="updatestatus(<?php echo $row['user_id']; ?>, 0);">Activate</label></td>
<?php } endwhile; ?>
//script.js
function updatestatus(id , status){
var ac_id = id;
var ac_st = status;
$.post("option.php", {"id" : ac_id, "com" : ac_st},
function(data){
if(data == "Updated"){
window.location.href = "adminPage.php";
}
}
);
}
Upvotes: 0
Reputation: 374
Try this
//adminPage.php
$getData = $mysqli->query("select * from 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";
}
}
);
});
Upvotes: 0
Reputation: 15213
I would suggest to change your ID's to classes (because ID's need to be unique).
If you have done that I would do this (I have changed your #optionLabel
to .optionLabel
and #userID
to .userID
:
$(".optionLabel").click(function(){
$.post("option.php", {"id" : $(this).prev('.userID').html(), "com" : $("#userActive").html()},
function(data){
if(data == "Updated"){
window.location.href = "adminPage.php";
}
}
);
});
Upvotes: 1