how to use json in php?

//test.php

<script type=”text/javascript” src=”http://code.jquery.com/jquery-2.1.0.min.js”></script>

<input type=”button” name=”show” id=”show” value=”Click TO Show Data
” />
<br>
Json Encode Data :
<div id=”json_output” style=”border:#000 1px solid;width:337px;height:337px;overflow:auto”>

</div>
Json Output Data of output variable :
<div id=”json_output_variable” style=”border:#000 1px solid;width:337px;height:337px;overflow:auto”>

</div>
<script type=”text/javascript”>
$(“#show”).click(function (){
var datastr=’format=json&id=37′;
$.ajax({
type:’POST’,
data:datastr,
url:’json.php’,
success:function(html)

{
$(“#json_output”).html(html);
var obj=JSON.parse(html);//decode the json output
$(“#json_output_variable”).html(obj.output);//u can use also obj.success and whatever u used in json.php
}
})

});
</script>

//json.php

<?php
$output=”<H1>Hello Json!!!  How R U?”;
$output.=”</h1> This is test mesaaage &nbsp;<br>Developed by :<a href=’http://thedarshak.com’>View My Website</a>”;

$output=json_encode($output);//encode ur data in json.
echo ‘{“output”:’.$output.’}’;
?>


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *