I have created one page in PHP where admin can see Password’s of all users.(Check below SS)
enter image description here
All rows are coming from DB (while loop)
Whenever i click on any Row’s EYE icon, it show’s only first rows password and not the row which i clicked on.
CODE :
<table id="example" class="table table-striped table-bordered margtbnone" style="width:100%">
<thead>
<tr>
<th>Employee ID.</th>
<th>Employee Name</th>
<th>Panel Password </th>
<th>Employee Status</th>
</tr>
</thead>
<tbody>
<?php
$Allusers=mysqli_query($con,"select * from `users` order by id asc");
while($users=mysqli_fetch_array($Allusers)){
?>
<tr>
<td><?php echo $users['id']; ?></td>
<td><?php echo $users['full_name']; ?></td>
<td>
<input type="password" name="password" id="id_password" value="<?php echo $users['password']; ?>" >
<i class="far fa-eye" id="togglePassword" style="margin-left: -30px; cursor: pointer;"></i>
</td>
<td><?php echo $users['status']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
const togglePassword = document.querySelector('#togglePassword');
const password = document.querySelector('#id_password');
togglePassword.addEventListener('click', function (e) {
// toggle the type attribute
const type = password.getAttribute('type') === 'password' ? 'text' : 'password';
password.setAttribute('type', type);
// toggle the eye slash icon
this.classList.toggle('fa-eye-slash');
});
</script>
Can you suggest changes required for the above issue
2
Answers
Here’s what I came up with:
So…
$i
(starting at zero)$i
is then fed into the ids of theid_password
andtoggle_password
elementsshowPassword
that takes in a number to identify the elements’ ids (the$i
).Hope it works 🙂
You should not use the
id
attribute at all for your password input elements. Use a class attribute instead. Then the resulting generated HTML could look something like below:I wrote the click-event handler as a delegated event handler. This way you are free to dynamically add further rows to your table without having to think about adding the event listeners to each new input field.
( I could not quite get your font awesome characters to show up, but that is a separate issue. )