$count=$dbo->prepare("select id,name,class as class1,mark from student where id=:id");
$count->bindParam(":id",$id,PDO::PARAM_INT,5);
$count->execute();
$row = $count->fetch(PDO::FETCH_OBJ);
$main = array('data'=>array($row));
echo json_encode($main);
In above code we have used json_encode function to generate the json string. Here is the output or the Json string
{"data":[{"id":"2","name":"Max Ruin","class1":"Three5","mark":"85"}]}
The above string is passed to JavaScript to parse the Json string and to get the data. here is the code.
var myObject = JSON.parse(httpxml.responseText);
var str="<table width='200' align=center>";
for(i=0;i<myObject.data.length;i++)
{
str = str + "<tr ><td >ID:</td><td>" + myObject.data[i].id + " </td></tr>";
str = str + "<tr ><td >Name:</td><td>" + myObject.data[i].name + " </td></tr>";
str = str + "<tr ><td>Class</td><td>"+ myObject.data[i].class1 + "</td></tr>";
str = str + "<tr><td>Mark</td><td>"+ myObject.data[i].mark + "</td></tr>";
}
str = str + "</table>" ;
document.getElementById("display").innerHTML=str;
Watch the first line in above code. We have used JSON.parse to create JavaScript object. We can also use eval() like this
var myObject = eval('(' + httpxml.responseText + ')');
It is not a good idea to use eval() as it can execute any JavaScript script so it is a security problem. However JSON.parse is not yet supported by all the advance browsers. So if it is not supported then we can keep another external JavaScript file json2.js downloaded from https://github.com/douglascrockford/JSON-js
$main = array('data'=>array($row),'value'=>array("bgcolor"=>"$bgcolor","message"=>"$message"));
$main = array('data'=>array($row));
echo json_encode($main);
Above two lines are required changes in main code. You can see we have added message, bgcolor ( background colour ) etc. to be posted to main script. Here it is how to get the data from JavaScript object in main script.
$main = array('data'=>array($row),'value'=>array("bgcolor"=>"$bgcolor","message"=>"$message"));
echo json_encode($main);
$sql="select * from student where id <5";
$row=$dbo->prepare($sql);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
$main = array('data'=>$result,'value'=>array("bgcolor"=>"#f1fff1","message"=>"All records displayed"));
echo json_encode($main);
Here is the Json string as output
{"data":[{"id":"1","name":"John Deo","class":"Four5","mark":"75","sex":"male"},
{"id":"2","name":"Max Ruin","class":"Three5","mark":"85","sex":"male"},
{"id":"3","name":"Arnold","class":"Three5","mark":"55","sex":"male"},
{"id":"4","name":"Krish Star","class":"Four5","mark":"60","sex":"male"}],
"value":{"bgcolor":"#f1fff1","message":"All records displayed"}}
https://www.plus2net.com/php_tutorial/student.php?str=ab
<?php
$dbhost_name = "localhost";
$database = "plus2net"; # database name
$username = "root"; # user name
$password = "pw"; # database password
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
//print "Error!: " . $e->getMessage() . "
";
//die();
}
/// Connection to Database is over ////
$str='%'.$_GET['str'].'%'; # Use parameter from Query string
$sql="SELECT * FROM student WHERE name LIKE :str";
$row=$dbo->prepare($sql);
$row->bindParam(':str', $str,PDO::PARAM_STR,10);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result); # display Json string
?>
Sample output is here for str=b
[{"id":7,"name":"My John Rob","class":"Fifth5","mark":78,"sex":"male"},
{"id":10,"name":"Big John","class":"Four5","mark":55,"sex":"male"},
{"id":14,"name":"Bigy","class":"Seven5","mark":88,"sex":"male"},
{"id":21,"name":"Babby John","class":"Four5","mark":69,"sex":"male"},
{"id":27,"name":"Big Nose","class":"Three5","mark":81,"sex":"male"},
{"id":28,"name":"Rojj Base","class":"Seven5","mark":86,"sex":"male"},
{"id":32,"name":"Binn Rott","class":"Seven5","mark":90,"sex":"male"}]
Using mysqli database connection
<?php
require "config.php"; // mysqli connection string
$sql="SELECT name,class,mark FROM student LIMIT 0,10";
$result = $connection->query($sql);
$row = array();
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
$row[] = $rs;
}
echo json_encode(array("student_data"=>$row));
$connection->close();
?>
var myObject = JSON.parse(httpxml.responseText);
We know the array name is data. So we will loop through the array to display each row of record of the array.
var str="<table><tr><th>ID</th><th>Name</th><th>Class</th><th>Mark</th></tr>";
for(i=0;i<myObject.data.length;i++)
{
str = str + "<tr><td>" + myObject.data[i].id + " </td><td>" + myObject.data[i].name + " </td><td>" + myObject.data[i].class + " </td><td>" + myObject.data[i].mark + "</td></tr>"
}
In addition to these array of records we have some single records stored in value. Here one sample to retrieve them
var message=myObject.value.message
Similarly another one
var bgcolor=myObject.value.bgcolor
<?Php
$my_data = new stdClass();
$my_data->name="plus2net";
$my_data->area="PHP";
echo json_encode($my_data);
?>
We will keep xmlhttprequest() in another file data1-read.php ( Only JavaScript code )
<DIV id="display"></DIV>
<script>
var httpxml = new XMLHttpRequest();
function state_check(){
if(httpxml.readyState==4){
//document.getElementById("display").innerHTML=httpxml.responseText;
var my_obj=JSON.parse(httpxml.responseText);
document.getElementById("display").innerHTML="NAME:" + my_obj.name+ " AREA:" + my_obj.area;
}
}
//////
httpxml.onreadystatechange=state_check;
httpxml.open("GET", "data1.php", true);
httpxml.send();
</script>
Or we can call this page by using jQuery $.getJSON("student-data.php", function(return_data){
We can also use Ajax to call function
Minh | 19-03-2015 |
How do you add a hyperlink onto the data that is displayed in the box? |