1
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$.getJSON('getfile.php', function(data) {
alert(data);
});
});

</script>
</body>
</html>

my php file-->getfile.php

<?php
$json='{"info":{"name":"ram","lname":"katara"}}';
$json = json_decode($json,true);
print_r($json);
?>

my output is-->

Array ( [info] => Array ( [name] => ram [lname] => katara ) )

How can i print this json data into my html page using javascript or ajax jquery.

4
  • Just echo the first line in your PHP, you don't need to decode or use print_r. Just echo '{"info":{"name":"ram","lname":"katara"}}';. Commented Jul 10, 2014 at 5:20
  • You do realize you're sending an array (PHP) and not json data as you're decoding it json_decode(). The real question here is What would you like to do with the json data you receive from your php script? Commented Jul 10, 2014 at 5:24
  • 1
    @Darren Question is definitely not well described, i agree, i think you need to json_encode(), see the answer and try it out. Commented Jul 10, 2014 at 5:31
  • even if i use json_encode(). i am not getting my output Commented Jul 10, 2014 at 5:49

3 Answers 3

1

It is the opposite you would do normally, take PHP objects and encode them into JSON.

php

$data=array('info'=>array('name'=>'ram',
                         'lname'=>'katara'
           ));
$json = json_encode($data);
echo $json; //{"info":{"name":"ram","lname":"katara"}}

Js

<script type="text/javascript" language="javascript">
$(document).ready(function() {
  $.getJSON('getfile.php', function(json) {
     $('#print').html('<p>First Name: ' + json.info.name + '</p>');
     $('#print').append('<p>Last Name : ' + json.info.lname + '</p>');
  });
});
</script>

<div id="print"></div>
Sign up to request clarification or add additional context in comments.

3 Comments

i want this data to be printed in another html file using javascript jquery or ajax..
See the below answer..!
@raviG I expanded on my answer
0

If i understood your question correctly, you want to put value of a php variable into javascript. For that, follow this:

Define your $json as global variable, at the top.

And in the Javascript:

<script> 

  var json = <?php echo $json; ?> ;   

</script>

Edit: You need to json_encode($json); instead of json_decode() ... this should output in correct format, then you can alert in Javascript however you like.

Comments

0

Yes AJAX query is a way to ask for data from a php file on the server to a HTML file.

The simplest POST AJAX query structure is below (you can use GET also):

$.POST("getfile.php",function(data) { 
    if(!data) {
       alert("No data received"); }
    else {
       $("YourOutputDivfromHTML").innerHTML += ......; //what ever to display from the response
       // or alert();

});

In the php file you should:

echo json_encode($json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.