1

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

2 Answers 2

3

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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?

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.