0

I'm a bit of an newbie in this - I've been trying with below code. But "login button" not working. Any help would be very much appreciated.

HTML CODE

  <div data-role="page" id="loginpage">
    <div id="header" data-theme="b" data-role="header">
        <h3>Login</h3>
    </div>
    <div id="wrapper">
        <form action="" name="mylogin">
            <div data-role="content">
                <div class="username" data-role="fieldcontain">
                    <label for="username"> Username </label> 
                    <input name="username" id="username" placeholder="enter username" value="" type="text">
                </div>
                <div class="psw" data-role="fieldcontain">
                    <label for="psw"> Password </label>
                    <input name="password" id="psw" placeholder="enter password" value="" type="text">
                </div>
                <input id="login" value="Login" type="button" name="Login" onclick="validationcheck();">
            </div>
        </form>
    </div>
</div>

JS

function validationcheck(){
var uname = "";
var pasw = "";
var username = document.getElementById("username").value;
var password = document.getElementById("psw").value;
if(username == uname && password == pasw){
    $.mobile.changePage("#page1");
}else{alert("login failed");
}}

sqlite database

function getregistdata(tx){
tx.executeSql('SELECT username, password FROM Registration', [], getlogin_success, transaction_error);}

function getlogin_success(tx, results){
var uname = "";
var pasw = "";
var len = results.rows.length;
alert("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++) {
uname = results.rows.item(i).username;
    pasw = results.rows.item(i).password;       
     }}
yazid
  • 11
  • 1
  • 5
  • 1
    what do you mean with "not working"? – Florian Semm Jun 27 '17 at 08:09
  • Perhaps this will help you understand what's wrong: https://stackoverflow.com/questions/9530954/how-to-call-external-javascript-function-in-html – davidl Jun 27 '17 at 08:29
  • Thanks @FlorianSemm ... when click "login button" with username=uname and password = pasw... not go to #page1. Uname and pasw is data from sqlite database. I try sqlite database good worked ondeviceready() – yazid Jun 27 '17 at 08:49

1 Answers1

0

var db;

try {
  db = openDatabase('app', '1.0', 'description', 2 * 1024 * 1024);
} catch (e) {
  console.log(e);
}

try {
  db.transaction(function(tx) {
    var sql;

    /*
    sql = `DROP TABLE Persons`;

    tx.executeSql(sql, [], function(tx, result) {
      console.log('RESULT:', result);
    }, function(tx, error) {
      console.log('ERROR:', error);
    });
    */

    sql = `
      CREATE TABLE IF NOT EXISTS Persons (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username VARCHAR(100) NOT NULL UNIQUE,
        password VARCHAR(100) NOT NULL 
      );
    `;

    tx.executeSql(sql, [], function(tx, result) {
      console.log('RESULT:', result);
    }, function(tx, error) {
      console.log('ERROR:', error);
    });

    /*
    sql = `
      SELECT * FROM Persons WHERE username='admin'
    `;

    tx.executeSql(sql, [], function(tx, result) {
      if (result.rows.length == 1)
      {
        var user = result.rows.item(0);
        console.log(user);
      }
    }, function(tx, error) {
      console.log('ERROR:', error);
    });
    */

    /*
    sql = `
      INSERT INTO Persons (
        username,
        password
      ) VALUES (
        'admin',
        'admin'
      );
    `;

    tx.executeSql(sql, [], function(tx, result) {
      console.log('RESULT:', result);
    }, function(tx, error) {
      console.log('ERROR:', error);
    });
    */

  });
} catch (e) {
  console.log(e)
}

function validationcheck() {
  if (document.mylogin.username.value == "") {
    console.log('please enter username');
    document.mylogin.username.focus();
  } else if (document.mylogin.psw.value == "") {
    console.log('please enter password');
    document.mylogin.password.focus();
  } else {
    var username = document.mylogin.username.value;
    var password = document.mylogin.password.value;
    try {

      db.transaction(function(tx) {
        tx.executeSql(`SELECT * FROM Persons WHERE username='${username}'`, [], function(tx, result) {
          if (result.rows.length == 1) {
            var user = result.rows.item(0);

            if (username == user.username && password == user.password) {
              console.log('LOGIN SUCCESS');
            } else {
              console.log('LOGIN FAILURE');
            }
          }
        }, function(tx, error) {
          console.log('ERROR:', error);
        });
      });

    } catch (e) {
      console.log(e);
    }
  }
}
<div data-role="page" id="loginpage">
  <div id="header" data-theme="b" data-role="header">
    <h3>Login</h3>
  </div>
  <div id="wrapper">
    <form action="" name="mylogin">
      <div data-role="content">
        <div class="username" data-role="fieldcontain">
          <label for="username"> Username </label>
          <input name="username" id="username" placeholder="enter username" value="" type="text">
        </div>
        <div class="psw" data-role="fieldcontain">
          <label for="psw"> Password </label>
          <input name="password" id="psw" placeholder="enter password" value="" type="text">
        </div>
        <input id="login" value="Login" type="button" name="Login" onclick="validationcheck();">
      </div>
    </form>
  </div>
</div>
  • Thanks @William Valhakis .... but isn't working ... Information..I use jquery mobile 1.4.5...when I click login button... referenceerror: db i not defined...why ? and ondeviceready, i use : var db = window.sqlitePlugin.openDatabase({name: 'amolefDB.db', location: 'default'}); ..thanks – yazid Jun 27 '17 at 11:59
  • Thanks... I modifikasi : var db =window.sqlitePlugin.openDatabase({name: 'amolefDB.db', location: 'default'}) in below "try" ...an success..thanks for all – yazid Jun 27 '17 at 13:45