0

I do an AXAX post like so:

$.ajax({ 
    type : 'POST',
    url : 'formprocess.php', 
    data: {formtype: 'load_all_names'},
    dataType : 'json',
    encode : true
}).done(function(data) {


});

The PHP page it posts to is formprocess.php, which looks like:

$return = array();

$return['names'][0]['name'] = 'Tom';
$return['names'][0]['age'] = 20;
$return['names'][1]['name'] = 'Dick';
$return['names'][1]['age'] = 30;
$return['names'][2]['name'] = 'Harry';
$return['names'][2]['age'] = 40;

echo json_encode($return);

Now, how do I get, for example, Harry's age?

$.ajax({ 
    type : 'POST',
    url : 'formprocess.php', 
    data: {formtype: 'load_all_designs'},
    dataType : 'json',
    encode : true
}).done(function(data) {

   alert(data.names[2].age); // Not working

});
  • EDIT: My formprocess.php file does echo the array.
MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • Try doing `console.log(data)` to see what you have, I'm guessing nothing as PHP's `return` doesn't output anything, you need `echo json_encode($return);` – adeneo Aug 31 '15 at 17:44
  • You need `echo json_encode($return)` ... `echo` is what you want [when you are using ajax](http://stackoverflow.com/questions/10107144/difference-between-php-echo-and-return-in-terms-of-a-jquery-ajax-call) – ODelibalta Aug 31 '15 at 17:44

0 Answers0