JSon

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” ” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd “>
<html xmlns=” http://www.w3.org/1999/xhtml “>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Request json test</title>
<script src=”http://code.jquery.com/jquery-1.5.js”></script&gt;
<script src=”json-jquery.js” type=”text/javascript”></script>
</head>
<body>
<a href=”#” id=”getdata-button”>Get JSON Data</a>
<div id=”showdata”></div>
</body>
</html>
[11:29:08 AM] Rajmohan | Infoquest: ———————————————–
[11:29:15 AM] Rajmohan | Infoquest: $(document).ready(function(){
//attach a jQuery live event to the button
$(‘#getdata-button’).live(‘click’, function(){
$.getJSON(‘json-data.php’, function(data) {
alert(data); //uncomment this for debug
alert (data.item1+” “+data.item2+” “+data.item3); //further debug
$(‘#showdata’).html(“<p>item1=”+data.item1+” item2=”+data.item2+” item3=”+data.item3+”</p>”);
});
});
});
[11:29:18 AM] Rajmohan | Infoquest: ————————————————
[11:29:26 AM] Rajmohan | Infoquest: <?php
//request data from the database
//code here to connect to database and get the data you want

//Example JSON format
{
“item1”: “I love jquery4u”,
“item2”: “You love jQuery4u”,
“item3”: “We love jQuery4u”
}

//return in JSON format
/*echo “{“;
echo “item1: “, json_encode($item1), “\n”;
echo “item2: “, json_encode($item2), “\n”;
echo “item3: “, json_encode($item3), “\n”;
echo “}”;*/
?>
[11:32:21 AM] Rajmohan | Infoquest: —————————————————————
[11:32:23 AM] Rajmohan | Infoquest: <!———————————————————————————————
Example client script for JQUERY:AJAX -> PHP:MYSQL example
by Trystan Lea : openenergymonitor.org : GNU GPL

I recommend going to http://jquery.com/ for the great documentation there about all of this
———————————————————————————————->
<html>
<head>
<script language=”javascript” type=”text/javascript” src=”jquery.js”></script>
</head>
<body>

<!———————————————————————————————
1) Create some html content that can be accessed by jquery
———————————————————————————————->
<h2> Client example </h2>
<h3>Output: </h3>
<div id=”output”></div>

<script id=”source” language=”javascript” type=”text/javascript”>

$(function ()
{
//——————————————————————————————-
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//——————————————————————————————-
$.ajax({
url: ‘api.php’,                  //the script to call to get data
data: “”,                        //you can insert url argumnets here to pass to api.php for example “id=5&parent=6”
dataType: ‘json’,                //data format
success: function(rows)          //on recieve of reply
{

for (var i in rows)
{
var row = rows[i];

var id = row[0];
var vname = row[1];
$(‘#output’).append(“<b>id: </b>”+id+”<b> name: </b>”+vname).append(“<hr />”);
}

}
});

});
</script>

</body>
</html>
[11:32:33 AM] Rajmohan | Infoquest: —————————————————————————–
[11:32:35 AM] Rajmohan | Infoquest: <?php

//————————————————————————–
// Example php script for fetching data from mysql database
// by Trystan Lea : openenergymonitor.org : GNU GPL
//————————————————————————–
$host = “localhost”;
$user = “root”;
$pass = “”;

$databaseName = “test”;
$tableName = “person”;

//————————————————————————–
// 1) Connect to mysql database
//————————————————————————–
//include ‘DB.php’;
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);

//————————————————————————–
// 2) Query database for data
//————————————————————————–
$result = mysql_query(“SELECT * FROM $tableName”);            //query
while ( $row = mysql_fetch_row($result) )
{
$data[] = $row;
}
echo json_encode( $data );

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s