0

I'm trying to access and display a variable within a json object on my page. Can anyone tell me why the variable gets displayed three times?

my_array.php

<?php

$my_data=array(name=>"john",age=>"30", city=>"copenhagen");

// sending output
header('Content-Type: text/json');
echo json_encode($my_data,true);
?>

My_page.php

<script>
$(document).ready(function() {
  $("button").click(function() {
    $.getJSON("my_array.php", function(data) {
      $.each(data, function(key) {
        $("#showdata").append(data.city);
      });
    });
  });
});
</script>

//Show the data further down the page.

<div id="showdata"></div>

This displays

copenhagencopenhagencopenhagen
3
  • Its: header("Content-Type: application/json"); Commented Mar 20, 2016 at 19:07
  • What do you want it to display? Commented Mar 20, 2016 at 19:10
  • 1
    Its displays three times because you have three keys. Commented Mar 20, 2016 at 19:11

5 Answers 5

1

That's because you're iterating over 'each' data item from the json response that you receive and there are 3 key=>value pairs in my_array.php

Removing "$.each(data, function(key) {} " will return the value 'city' only once

    $(document).ready(function(){
        $("button").click(function(){
            $.getJSON("my_array.php",function(data){
                $("#showdata").append(data.city);
            });
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

1

use this

my_array.php

<?php

$my_data = array(
    name    =>  "john",
    age     =>  "30",
    city    =>  "copenhagen"
);

// sending output
header('Content-Type: application/json');
echo json_encode($my_data, true);

?>

My_page.php

<div id="showdata"></div>
<button>Click Me!</button>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(e){
        $.getJSON("test.php", function(data){
            console.log(data);
            $("#showdata").append(data.city);
        });
    });
});
</script>

this will give you only one copenhagen.

hope it helps...

Comments

0

First things first:

Set either:

header("Content-Type: application/json");

Above:

echo json_encode($my_data,true);

On your php file.

Or use the following snippet on your javascript:

$.getJSON("my_array.php",function(data)
{
  data=JSON.stringify(data);
  $.each(data, function(key) 
  {
    $("#showdata").append(data.city);
  });
});

Furthermore either in both ways above the returned data is an Object so in order to return correctly on your php file the data must be:

$my_data=array(array(name=>"john",age=>"30", city=>"copenhagen"));

Note: Associative arrays on php's json_encode turned into Objects. Non associative arrays on json_encode still remain an array

Comments

0

Im guessing because you have got three buttons on the page and the $.each takes the selector try:

$("button").click(function(){
  $.getJSON("my_array.php",function(data){
    $("#showdata").append(data.city);
  });
});

Comments

0

you're iterating 3 times because of the three keys you have within the JSON object,

$.getJSON( "ajax/test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    console.log( "key + " " + val" );
  });

take a look at the JQuery documentation for further info.

http://api.jquery.com/jquery.getjson/

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.