If someone could assist me please. I'm doing a jquery Ajax post, for some reason the Json object isn't working so just returning a php array instead

$.post
(  
  "classes/RegisterUser.php",  
  $("#frmRegistration").serialize(),  
  function(data)
  {  
   alert(data);  
  } 
);

The data is returned to Javascript 100% as

array
(
   [key]=>value
   [Name] => SomeOneName
   [Surname] => SomeOneSurName
)

How would i go about getting the value of Surname in Javascript?

Thanks for your assistance? Regards

share|improve this question
feedback

2 Answers

Expanding on The MYYN's answer, after you get your script to return JSON, you must specify that you're receiving JSON and act accordingly. You can do this with .ajax():

$.ajax({
    type: 'post',
    url: 'classes/RegisterUser.php',
    data: $("#frmRegistration").serialize(),
    dataType: 'json',
    success: function(obj) {
        // This alerts SomeOneSurName
        alert(obj.Surname);
    }
});
share|improve this answer
Thanks Tatu, worked like a charm! – user232840 Dec 28 '09 at 9:47
feedback

Maybe your PHP script should return json (right now it seem to return something like var_dump($some_ary);. A proper way to do this is via php's json_encode.

share|improve this answer
I used the json_encode method which returns the databack 100%, when i then try to display the data in javascript as e.g. alert(data.Name); I get the error undefined? – user232840 Dec 28 '09 at 9:28
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.