-1

I'm working on a little project, and I want to make a simple login system using JavaScript.

To clarify:

I have a main file called login.html for example, in which I will have a form for the Username and Password. I want to make it so if the user types "User1" for the Username field and "Password1" for the Password field it will take him to user1.html page, but if he types "User2" for the Username field and "Password2" for the Password field it will take him to user2.html page.

I know that it isn't safe using JavaScript for this, but since this is just a small private project I'm working on, I don't see many problems with using JavaScript for this.

My current code is:

function Getstats() {
  window.status = ('Login')

  var location = '';
  if (iName == 'User1' && AccId == 'Password1') {
    location = ("user1.html");
  } else if (iName == 'User2' && AccId == 'Password2') {
    location = ("user2.html");
  } else alert('Wrong Username or Password. Please try again.');

  this.location.href = location;
}
<form action="" name="iAccInput">
  <input placeholder="Username*" type="text" name="iName" required>
  <input placeholder="Password*" type="password" name="iAccID" required>
  <button onclick="Getstats()">Login</button>
</form>

But it isn't working so far. I've tried researching it also a bit, but I couldn't find anything related to my issue (anything helpful at least).

And yes, I am trying to avoid using PhP or SQL for this.

  • 1
    To start, add `type="button"` to your button (or get rid of the `
    ` element) so it's not posting a form. Once you do that, you can see an error in the browser console. The variables `iName` and `AccId` are not defined.
    – David May 12 '22 at 12:55
  • 1
    Where do the variables `iName` and `AccId` get set? – phuzi May 12 '22 at 12:55
  • You have redundant brackets. `location = ("user1.html");` could be improved to `location = "user1.html";` – Lee Taylor May 12 '22 at 13:03

0 Answers0