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.
2
  • 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); Commented Aug 31, 2015 at 17:44
  • You need echo json_encode($return) ... echo is what you want when you are using ajax Commented Aug 31, 2015 at 17:44

0

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.