如果我单击“发送”按钮,则会在客户端 sql 数据库中放置一条消息并显示一条通知。
但是,这只有在我先刷新页面时才有效!
如何使该功能立即开始工作?我已经尝试将函数“addMessage”放在 $(document).ready(function() 中,但随后该函数完全停止工作。请参见下面的代码:
<script>
var db;
$(document).ready(function() {
//Opens the database
try
{
if (!window.openDatabase) {
alert('Not supported -> Please use a webkit browser');
} else {
var shortName = 'Rvpk';
var version = '1.0';
var displayName = 'The Rvpk databse';
var maxSize = 3072*1024; // = 3MB in bytes 65536
db = openDatabase(shortName, version, displayName, maxSize);
}
}
catch(e)
{
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}return;
}
//If needed creates the messagetable
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS message (id INTEGER PRIMARY KEY, name)')
});
});
//The function addMessage takes the entered message from the message inputfield and places it inside the messagetable
function addMessage(){
var message = $('input:text[name=message]').val();
db.transaction(function (tx) {
tx.executeSql("INSERT INTO message (id, name) VALUES (NULL, ?)", [message]);
});
console.log("Message "+ message +" added to database");
//Shows a notification that sending the message was a success
alert('The message has been sent');
//Update the messages
updateMessages();
}
</script>
</head>
<body>
<?php include("includes/header2.php"); ?>
<div data-role="content">
<form>
<fieldset>
<label id="messagelabel" for="message">Message:</label>
<input id="message" type="text" name="message" value="This can't go on like this">
<button onClick="addMessage()" data-theme="g">Send</button>
</fieldset>
</form>
</div>