Reputation: 19
I've been working on a college project for which I need to use dates and enter them in the database (the built-in one in the Chrome browser). To be specific, I'll be taking a date from the user and enter it in the database.
The HTML code goes like this:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
</head>
<body>
<input type="date" id="dt">
<input type="button" id="submit" value="INSERT DATE">
</body>
<script type="text/javascript" src="js/index.js"></script>
</html>
And the JavaScript:
var db = openDatabase("Dates", "1.0", "Test Dates", 200000);
var createStatement = "CREATE TABLE IF NOT EXISTS Date (sampledate DATE)";
var insertStatement = "INSERT INTO Date (sampledate) VALUES (?)";
db.transaction(function(tx) {
tx.executeSql(createStatement, []);
});
$(document).ready(function() {
$("body").fadeIn(2000);
$("#submit").click(insertdate);
});
function insertdate() {
var datetemp = $("#dt");
db.transaction(function(tx) {
tx.executeSql(insertStatement, [datetemp])
})
alert("SUCCESS");
}
This is what I get in the database:
So it'd be really great if I get some help!
Upvotes: 0
Views: 301
Reputation: 77
You are passing [datetemp]
as an object.
function insertdate()
{
var datetemp = $("#dt");
db.transaction(function(tx)
{
tx.executeSql(insertStatement, [datetemp]) // your passing the object here
})
alert("SUCCESS");
}
You need to iterate through this object and get the values. Write them to an array.
[datetemp].forEach(function(value){ // iterating through it . . .
// do stuff here . . . (i.e inert values in to array)
});
Then add database
var insertStatement = "INSERT INTO Date [dbo].[sampledate] (col0, col1, col2
// ect . . .) VALUES ([array].day, [array].month, [array].year // ect . . .)";
function insertdate()
{
var datetemp = $("#dt");
db.transaction(function(tx)
{
tx.executeSql(insertStatement) // just take the object out
})
alert("SUCCESS");
}
OR
Change the insert command and put that after you have the [datetime]
this would be simplier :)
var insertStatement = "INSERT INTO Date [dbo].[sampledate] (col0, col1, col2
// ect . . .) VALUES ([datetemp].day, [datetemp].month, [datetemp].year //
ect . . .))";
function insertdate()
{
var datetemp = $("#dt");
db.transaction(function(tx)
{
tx.executeSql(insertStatement) // just take the object out
})
alert("SUCCESS");
}
Upvotes: 0
Reputation: 340
You are passing $("#dt") to the database, which is an jQuery encapsulated object that contains your input DOM object.
Just change this line:
var datetemp = $("#dt");
To this:
var datetemp = $("#dt").val();
It should work fine!
Hope it helps!
Upvotes: 1
Reputation: 146
You are passing the datetime object instead of text box value use Val function in Jquery var datetemp = $("#dt").val();
Upvotes: 0