yank yank - 6 months ago 33
Javascript Question

How to display json_encode value in html?

I am very new to ajax and json stuff. Here is jQuery:

$('#myHref').change(function(){
$.get('get_projectName.php',{id:value},function(data)
{
data = JSON.parse(data);
$( '#detail' ).val(data.a);
$( '#sector' ).val(data.b);
$( '#unit' ).val(data.c);
});
});


In
get_projectName.php


$a=5;$b=10;$c=15;
json_encode(array(
'a' => $project_code,
'b' => $b,
'c' => $c
));


I want to display value of


$a, $b and $c


In div

enter image description here


detail, sector and unit


But I cannot display them.

Answer

You are missing printing like echo. Your PHP should be:

$a=5;$b=10;$c=15;
echo json_encode(array(
    'a' => $a,
    'b' => $b,
    'c' => $c
    ));

JQuery:

$.get('1.php',{id:value},function(data)
    {  data = JSON.parse(data); 
      $( '#detail' ).html(data.a);
      $( '#sector' ).html(data.b);
      $( '#unit' ).html(data.c);  
    });