I am trying to make a query to an SQL database and I can't figure out why my code won't work. When I call the javascript function:
function calledfunction(){
var date = new Date(document.getElementById("datetime1").value);
var dateformat = "'"+date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() +":" + date.getMinutes()+"'";
alert("Value is: " + dateformat);
microAjax("genjsonphp.php?stdt="+dateformat, function(data) {
//edited out
}
I get the alert: Value is: '2011-12-6 0:0'
When I copy the value in the alert and paste it so the above code becomes:
function calledfunction(){
var date = new Date(document.getElementById("datetime1").value);
var dateformat = "'"+date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() +":" + date.getMinutes()+"'";
alert("Value is: " + dateformat);
dateformat = '2011-12-6 0:0';
microAjax("genjsonphp.php?stdt="+dateformat, function(data) {
//edited out
}
Then the code works fine. Does anyone have an idea what is going wrong?
You're explicitly quoting the variable string:
"'"+date.getFullYear() ...
so you're sending
genjsonphp.php?stdt='2011-12-6 0:0'
(note the single quotes)
In the second case you're sending:
genjsonphp.php?stdt=2011-12-6 0:0
this "genjsonphp.php?stdt="+'2011-12-6 0:0'
parses to: "genjsonphp.php?stdt=2011-12-6 0:0"
while "genjsonphp.php?stdt="+dateformat
parses to: "genjsonphp.php?stdt='2011-12-6 0:0'"
Watch the quotes.