TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-06-2010, 07:45 PM   #1 (permalink)
The Visitor
 
Join Date: Dec 2010
Posts: 2
Thanks: 0
lee1963 is on a distinguished road
Default help with php json and javascript

right i no this must be simple to achieve but i cnat get my head round it.
What i want to no is after i create my array in php i encode it with "json_encode and echo it out.
it comes out in the right format
but how do i use it in the javascript tags.
Any help would be greatly appreciated

Code below

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("herefordinfo") or die(mysql_error());

$dbQuery = 'SELECT * FROM companies';
$dbResult = mysql_query($dbQuery) or die ("Could not read data because " . mysql_error());
$RowCount = mysql_numrows($dbResult);

while ($pubs = mysql_fetch_assoc($dbResult))
{
$array[] = array('id'=>$pubs['id'],'companyname'=>$pubs['companyname'],'companyinfo'=>$pubs['companyinfo'],companyimage=>$pubs['companyimage']);

}
//echo '<pre>' . print_r($array,true) . '</pre>';
echo '<pre>' . json_encode($array) . '</pre>';

?>
<html>
<head>

<script type="text/javascript">

</script>

</head>
<body>
</body>
</html>

lee1963 is offline  
Reply With Quote
Old 12-06-2010, 08:29 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

You would set the output of the PHP as a variable in the javascript. You then could access the data using the javascript variable/object.

PHP Code:
<html>
<head>

<script type="text/javascript">
<?php
mysql_connect
("localhost""root""password") or die(mysql_error());
mysql_select_db("herefordinfo") or die(mysql_error());

$dbQuery 'SELECT * FROM companies';
$dbResult mysql_query($dbQuery) or die ("Could not read data because " mysql_error());
$RowCount mysql_numrows($dbResult);

while (
$pubs mysql_fetch_assoc($dbResult))
{
$array[] = array('id'=>$pubs['id'],'companyname'=>$pubs['companyname'],'companyinfo'=>$pubs['companyinfo'],companyimage=>$pubs['companyimage']);

}
//echo '<pre>' . print_r($array,true) . '</pre>';
echo 'var data = ' json_encode($array);

?>

alert(data[0].companyname);

// if you have firebug
console.dir(data);

</script>

</head>
<body>
</body>
</html>
__________________
Eric
wGEric is offline  
Reply With Quote
Old 12-06-2010, 09:01 PM   #3 (permalink)
The Visitor
 
Join Date: Dec 2010
Posts: 2
Thanks: 0
lee1963 is on a distinguished road
Default cheers

you have just shot straight to th top of my xmas card list, many thx ... lee
lee1963 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easily Format JSON using PHP and Interpret using Javascript Wildhoney Advanced PHP Programming 9 06-22-2009 01:12 AM
Secure AJAX Server Scripts trmbne2000 General 4 12-07-2007 12:14 AM
JSON invalid label Wildhoney Javascript, AJAX, E4X 6 11-07-2007 11:58 AM


All times are GMT. The time now is 12:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design