Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  112] [ 6]  / answers: 1 / hits: 28405  / 12 Years ago, wed, june 27, 2012, 12:00:00

I cannot seem to get user entered data into a table and then printed.


Here is my code thus far:


<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
function submit() {
var input = document.getElementById("save_name").value;
localStorage.setItem("inputed_name", input);
var msg;
db.transaction(function (tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log varchar(50))');
tx.executeSql('delete from LOGS'); // Clears table (for debugging)

tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, localStorage.inputed_name)');
msg = '<p>Log message created and row inserted.</p>';
document.querySelector('#status').innerHTML = msg;
});

db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>Found rows: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++) {
msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}, null);
});
}
</script>
</head>
<body>
<div id="status" name="status">Status Message</div>
<p>Enter data: <input type="text" id="save_name" name="inputed_name" /><br/></p>
<p><button onclick="submit()" type="button">Submit Data</button></p> <!--Should print data from the table-->
</body>
</html>

I got the "template"/start for this from here


Solved Below


Now I want to add two variables to my table, the original log variable and a second to act as a time stamp


function submit() 
{
var input = document.getElementById("save_name").value;
var msg;
var time_stamp = new Date();

db.transaction(function (tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id auto_increment, date_time varchar(128), log varchar(64))');
tx.executeSql('INSERT INTO LOGS (log, date_time) VALUES (?, ?)', [input, time_stamp]); // <--Problem here!
msg = '<p>Log message created and row inserted.</p>';
document.querySelector('#status').innerHTML = msg;
});
}

The rest of the code is the same as above.
I would think this would work but when I test it nothing gets in the table.


More From » sql

 Answers
22

I think you just need to change the way you're getting and inserting your values (also, you had a syntax error in the SQL on this line):



var input = document.getElementById('someId').value;

...
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, ?)', [input]);
...


I think you can forget about the localStorage variable altogether, unless you need to persist those values.


[#84621] Tuesday, June 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kayap

Total Points: 634
Total Questions: 83
Total Answers: 110

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;