| | |
How do I use PHP to create a JSON object?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 0
I am trying to use a Dojo (ajax) table on my page that has detail rows in which I want to draw detail tables. The tables have a format event which calls formatDetail. In this event I would like to build a simple table using JSON data.
My problem is how do I use PHP to create the JSON data object so that my formatDetail javascript can use it?
My test code so far is, in the header: And in the body of the page: When I try this, the js alert displays "Length is undefined"
What am I doing wrong?
My problem is how do I use PHP to create the JSON data object so that my formatDetail javascript can use it?
My test code so far is, in the header:
javascript Syntax (Toggle Plain Text)
function formatDetail(inWatchId, inRowIndex) { alert("Length is: " + myJSON.length); }
php Syntax (Toggle Plain Text)
$stmt = sqlsrv_query($connection,$query); while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $arr[] = $row; } $objJSON = '{"sample":'.json_encode($arr).'}'; echo "<script>var myJSON = $objJSON;</script>";
What am I doing wrong?
Don't try to mix creating your own json strings
php Syntax (Toggle Plain Text)
$objJSON = array('sample' => null); // whatever $objJSON['sample'] = $arr; $objJSON = json_encode($objJSON); echo '<script type="text/javascript">var myJSON = ' . $objJSON . ';</script>
Last edited by ShawnCplus; Sep 2nd, 2009 at 4:13 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
![]() |
Similar Threads
- Having Difficulty Parsing Through a Nested JSON Object Using Prototype Framework (JavaScript / DHTML / AJAX)
- how to create the dynamic object using C# (C#)
- Iterating over JSON object (JavaScript / DHTML / AJAX)
- Can you create a class object based on user input? (C++)
- Should you create a class object for each member function of a class? (C++)
- how to create ActiveX Object (C#)
- how to create more objects in object pool (Java)
- Create a proxy object (Java)
- Can php allow me to create whatever.homepage.com address (PHP)
Other Threads in the PHP Forum
- Previous Thread: uploading to web server
- Next Thread: PHP Table Pagination without Database
Views: 484 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date development directory display download dynamic echo email error file files folder form forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






