Cordova WebSQL - onSubmit [英] Cordova WebSQL - onSubmit
问题描述
我刚刚接触Cordova,目前正在使用它。我现在创建一个登录脚本,但是当我提交一个表单,它看起来像数据库不会反应。
I'm new to Cordova and currently working with it. I'm creating a login script at the moment, but when I submit a form it looks like the database won't react.
以下是我正在尝试要执行以下操作:
The following is what I'm trying to do:
- 加载API时,创建一个表并插入一条记录。
- 当用户提交表单时,请检查插入的数据是否等于表格的数据。
- 如果匹配,请转到新页面。
当我插入一些无用的东西时,我甚至没有得到警告 invalid ...
。我在启动应用程序时收到成功!
提醒。
I'm not even getting the alert invalid ...
when I inserted something useless. I'm getting the success!
alert when I start-up the app.
HTML:
<form id="login" onsubmit="check_login();" class="form-signin">
<input id="username" type="text" class="form-control" placeholder="Name" required autofocus>
<input id="password" type="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">
Sign in
</button>
</form>
JAVASCRIPT:
JAVASCRIPT:
var db;
function check_login() {
var username = $("#username").val();
var password = $("#password").val();
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM LOGIN WHERE username=? AND password=?', [username, password], function(tx, results) {
if (results.rows) {
window.location = "main.html";
} else {
alert("Invalid username or password");
}
}, null);
});
}
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
// Populate the database
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS LOGIN');
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGIN (id unique, username, password)');
tx.executeSql('INSERT INTO LOGIN (id, username, password) VALUES (1, "Admin", "Admin")');
}
// Transaction error callback
//
function errorCB(tx, err) {
alert("Error processing SQL: " + err.code);
}
// Transaction success callback
//
function successCB() {
alert("success!");
}
推荐答案
解决了!首先我需要防止提交按钮的默认事件。第二我不认为 results.row
足够了,但这将做 if(results.rows.length> 0)
。
Solved it ! first i needed to prevent the default event of the submit button. second i didn't think results.row
was enough but this will do if (results.rows.length > 0)
.
我目前使用的代码:工作太棒了!
The code i use at the moment: works great !
function check_login() {
event.preventDefault(); // cancel default behavior
var username = $("#username").val();
var password = $("#password").val();
db.transaction(function(tx) {
alert(tx + "test");
tx.executeSql('SELECT * FROM LOGIN WHERE username=? AND password=?', [username, password], function(tx, results) {
if (results.rows.length > 0) {
window.location = "main.html";
} else {
alert("Invalid username or password");
}
}, errorCB);
});
}
这篇关于Cordova WebSQL - onSubmit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!