javascript - PHP to JSON on client side -
i'm using rob monies 'jquery week calendar' build calendar application.
i have mssql db table named 'dates' , following fields:
id start end title
i query db in php , pass results javascript, javascript render events in browser.
the javascript needs receive event details in json format per following example:
{"id":1,"start": 2010-10-09t13:00:00,"end":2010-10-09t14:00:00,"title":"lunch mike"}, {"id":2,"start": 2010-10-10t13:00:00,"end": 2010-10-10t14:00:00, "title":"dev meeting"}
so far, keep things simple i've returned 1 row db @ time, - need application able render multiple events, stored in database.
i've tried using json_encode()
put values var , pass them javascript var called db_events - if return db_events in alert box on client side see following;
{"id":1, "start":2010-10-09t13:00:00, "end":2010-10-09t14:00:00,"title":"lunch mike"}
which looks ok, when in code:
events : [db_events]
it doesn't work :(
if take php out of equation, , following on client side:
var db_events = {"id":1, "start":2010-10-09t13:00:00, "end":2010-10-09t14:00:00,"title":"lunch mike"};
and return db_events in alert box, get:
[object] [object]
but when this:
events : [db_events]
it works!
back php …
if put sql result php vars follows:
$id = id; $start = start; $end = end; $title = title;
and pass vars following js vars:
js_id js_start js_end js_title
and on client side:
var db_events = {"id":js_id, "start":js_start, "end":js_end,"title":js_title}; events : [db_events]
that works.
as can tell - i'm new , missing basic.
any help, advice or information appreciated :)
many thanks
tim
when string representation of object means you've exported string not object.
when
[object] [object]
means object, right!
you can jsonp if json url:
// send request via <script> tag var src = "..."; // url of json output var head = document.getelementsbytagname("head")[0]; var script = document.createelement("script"); script.setattribute("type", "text/javascript"); script.setattribute("src", src); head.appendchild(script);
or parse string json parser: https://github.com/douglascrockford/json-js/blob/master/json2.js
var db_events = json.parse( ...json output string... );
or directly passing php inline <script>
block in page:
echo "<script>"; echo "var db_events = " . json_encode( ... ) . ";"; echo "</script>";
Comments
Post a Comment