Reputation: 125
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
detail, sector and unit
But I cannot display them.
Upvotes: 0
Views: 1749
Reputation: 106
If I understand, you've managed to get the PHP to echo the results in JSON format and you're pulling this through to the page with the DIV via Ajax.
You'll want to:
1) Download the entire JSON response
2) Parse the response to split the three answers
3) Display the answers individually in the divs
//Step 1: Download the entire JSON response
$.get( "get_projectName.php", function( data ) {
//Step 2: Parse the response
result = JSON.parse(data);
//Step 3: Load the responses into each div
$( '#detail' ).html( result['a']);
$( '#sector' ).html( result['b']);
$( '#unit' ).html( result['c']);
});
If the "Detail", "Sector" and "Unit" elements are inputs and not divs, use .val instead of .html
If you need any more info, don't hesitate to let me know! :)
Upvotes: 1
Reputation: 13293
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);
});
Upvotes: 4