tags:

views:

78

answers:

2

i use $.getJSON and here is my php file echo:ing back jsonstring

for($i=1; $i<=10; $i++)
{
    $nr[] = "nr" . $i;
}

$nr = json_encode($nr);

echo "{'result': 'error', 'count': '$nr'}";

how do i loop all the nr through with jquery html()?

i want to echo it back to webpage like:

nr 1 nr 2 nr 3 nr 4 nr 5 nr 6 nr 7 nr 8 nr 9 nr 10

+1  A: 

In jquery, eval the "count" like

array_data=eval(json_data["count"])

php return this

{'result': 'error', 'count': '["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]'}

Once you eval "count"

array_data will be ["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]

After that you can loop array_data

S.Mark
what if the count variable holds different string values?
never_had_a_name
ok, its same way, I also updated my answer.
S.Mark
A: 
$.getJSON('file.php', function(data){
  for(var i=0; i<data.count.length; i++){
    alert(i+": "+data.count[i]);
  }
});

edit: The problem is that the way you store it, the php array is stored as a string in the json. Try the following instead:

for($i=1; $i<=10; $i++)
{
    $nr[] = "nr" . $i;
}
$ json = array('result' => 'error', 'count' => $nr);
echo json_encode($json);
Marius