Link to home
Create AccountLog in
Avatar of Wanda Marston
Wanda MarstonFlag for Canada

asked on

How do I create a Login page that allows the viewer to "show the password"?

The following is my code. This page is set up as an include file.
<?php

// This script displays the login form for the TBRMobile site.

// Create an empty error array if it doesn't already exist:
if (!isset($login_errors)) $login_errors = array();

// Need the form functions script, which defines create_form_input():
require_once ('./includes/form_functions.inc.php');

?>

<form action="Login.php" method="post" accept-charset="utf-8">

<h2>LOGIN</h2>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
    
 <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>
    
<?php if (array_key_exists('login', $login_errors)) {
echo '<span class="error">' . $login_errors['login'] . '</span>'; }?></p>

<p><label for="email" class="SmallHeading"><strong>Email Address</strong></label>
<p><?php create_form_input('email', 'text', $login_errors); ?></p>

<p><label for="pass2" class="SmallHeading">
  <i class="far fa-eye" id="togglePassword" style="margin-left: -30px; cursor: pointer;"></i>    
<strong>Password</strong></label></p>
<p><a href="ForgotPassword.php"><h2>Forgot Your Password?</h2></a></p>

<p><?php create_form_input('pass', 'password', $login_errors); ?></p>
      
<button class="button" style="vertical-align:left"><span>Login &rarr;</span></button><br />
<br />
</form>

Open in new window

SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Wanda Marston

ASKER

The issue is that the "eye" is not showing up on the Login page so it just looks like it doesn't exist.
User generated image
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Here is a simple way to do it (working sample here https://www.marcorpsa.com/ee/t3971.html)
CSS
.toggle-button input[type='password'] + button:after {
  content: "0";
  display: inline-block;
}
.toggle-button input[type='text'] + button:after {
  content: "X";
}

Open in new window

HTML
<form>
  <div class="toggle-button">
    <input type="password" name="password">
    <button 
       type="button" 
       onmousedown="this.form.password.type='text'" 
       onmouseup="this.form.password.type='password'">
    </button>
  </div>
</form>

Open in new window

The solution flips the type on the password input when the mousedown and mouseup events fire.
We use some simple styling to change the look of the button.
Depending on your CSS framework you can adjust this to show an eye icon and crossed out eye icon (or similar).
Okay, thanks for your comments.

I don't know if this makes a difference but I am on a mac. When I right click on the web page in the browser it does not mention anything about a console.

I have given you all the code that I have so I feel that I just have put a line of code in the wrong place.

Other than that, I don't think I want something that toggles. I would like the password to be visible to the user and be visible for a few seconds at least.


Console => Inspect Element
F12 is another way to get there.
This is what came up under Inspect - then console
User generated image
Is this to your original code or to the code I posted above.

What does the rendered HTML look like?
1. View Source
2. Copy HTML
3. Attach / paste here
Wanda,

I seem to remember the code you have for hiding and unhiding the password with that icon from another question here.  What is missing is a reference to jquery on your page.

Add jquery to your page https://code.jquery.com/  load it just before.
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
         <script src="assets/js/main.js"></script>

Open in new window

Your other option is to change the field from a password field to a text field. My assumption is it is in this line
<p><?php create_form_input('pass', 'password', $login_errors); ?></p>

Open in new window