1

I'm using PHP5, MySQL, JavaScript and Fusion Charts.

I have the following time value returned from a database: 2011-12-19 12:00:00

After taking it from the database, I try to pass it through a JavaScript strURL to get it to another page where I can make further database calls using this value. The problem I have is that the JavaScript fails whenever I send it through a date/time. I can send through any other value type and it works so the problem seems to be with the time stamp. I've tried converting it to string before it is passed (just to be sure it's not one already) and that doesn't work. I'm guessing it's to do with the characters within the value. Any idea how to get around this?

The database call in PHP and then sending fields into the JavaScript:

$strQuery = "SELECT unit, watts, time, device, siteid FROM inverter WHERE time = '2011-12-19 12:00:00' AND siteid = '842'";
$result2 = mysql_query($strQuery) or die(mysql_error());

if ($result2) {
    while($ors2 = mysql_fetch_array($result2)) {
        $thetime = (string)$ors2['time'];
        $strXML .= "<set color='58ACFA' label='" . $ors2['device'] . "/" . $ors2['unit'] . "' value='" . $ors2['watts'] . "' link='javaScript:updateChart(" . $ors2['unit'] . " , " . $ors2['device'] . " , " . $ors2['siteid'] . " , " . $thetime . ")'/>";
    }
}

And then the JavaScript function:

function updateChart(first, second, third){
    //DataURL for the chart
    var strURL = "FactoryData.php?factoryId=" + first + "&device=" + second + "&siteid=" + third;
    FusionCharts("FactoryDetailed").setXMLUrl(strURL);
}
3
  • Some sample code would be great, have you tried urlencoding the value if you send it via url parameter? Commented Jan 5, 2012 at 10:12
  • Maybe I'm blind but I don't see any date/time variable in the Javascript function you posted... I see 4 parameters in your call to updateChart() but the method receives only 3 parameters, and doesn't refer to any datetime value. Can you double check your example? Commented Jan 5, 2012 at 10:56
  • Yea sorry this is why i didn't post my code in the first place - because it's a mess! i've been taking stuff out and putting stuff in, the graph should load even without that variable in place because at the moment i'm not calling it on the other side of the javascript. If i change the " . $thetime . " variable to anything else then it lets the javascript load, only when it's a timestamp does it not load the graph. Commented Jan 5, 2012 at 11:03

2 Answers 2

1
link='javaScript:updateChart(" . $ors2['unit'] . " , " . $ors2['device'] . " , " . $ors2['siteid'] . " , " . $thetime . ")'

This JavaScript becomes something like

updateChart(341, 454, 842, 2011-12-19 12:00:00);

The first three arguments are valid numbers (I assume the IDs are integers), but the fourth argument causes a syntax error. What you need to do is wrap it in quotes to make it a string:

link='javaScript:updateChart(... " , \"" . $thetime . "\")'
                                     ^^                ^^

Now the JavaScript should be like this:

updateChart(341, 454, 842, "2011-12-19 12:00:00");
Sign up to request clarification or add additional context in comments.

1 Comment

Yep that's the one :D although found out there was more quotes involved so need 3 backslashes either side....nightmare!!
0

You can either convert the date to a timestamp with something like

new Date('2011-12-19 12:00:00').getTime()

or encode it using encodeURIComponent('2011-12-19 12:00:00')

The latter is probably the better solution, as it works with all kinds of values, not only dates. You can use decodeURIComponent if you want to read the parameters on the client side, that should already be working fine on the server side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.