1

I am getting ajax response in array format from php url. How to extract array response values in jQuery? FYI:

PHP array is:

$response = array('msg' => 'Hello', 'html' => '<b>Good bye</b>');

I am getting $response array in my ajax response. i.e.

var promo = "promo=45fdf4684sfd";
$.ajax({
    type: "POST",
    url: baseJsUrl + "/users/calc_discount",
    data: promo,
    success: function (msg) { // I am getting $response here as ajax response.
        //alert(msg);

        // Here I want to check whether response is in array format or not. if it is in array format, I want to extract msg here and want to use response array values.
    }
});

Let me know answer pls. Thanks.

4 Answers 4

8

You should echo that $response with json_encode().

You should probably set dataType: 'json' too inside the object literal you send to $.ajax().

Then you can access it natively with JavaScript using the dot operator inside your success callback...

function(msg) {
    alert(msg.html);
}

BTW, this line...

$response = array(['msg'] => 'Hello', 'html' => '<b>Good bye</b>');

... isn't valid PHP. Remove the brackets from the first key.

Sign up to request clarification or add additional context in comments.

2 Comments

Does your example really work? I have to parse the response first to got a result like this: var obj = jQuery.parseJSON(msg); alert(obj.html);
@Hexodus Depends on what jQuery function you're using to read it. If you tell it the response type is json, it will automatically parse it (which you should be doing).
1

My favorite solution for this is to encode array with PHP's function json_encode() so jquery will be happy to parse it.

Comments

0

I presume that you mean a JSON response, like this:

{"msg":"Hello","html":"<b>Good bye<\/b>"}

This is actually a native JS object, so you can use it right away like this:

success: function(msg){
   alert(msg.msg);
   alert(msg.html);
}

You can also use the jQuery.each() function to loop over all properties of the JSON object if you need to:

jQuery.each(msg, function(key, val) {
  alert(key + "=" + val);
});

Comments

0

and If you do not have control over the PHP output then you can use another method to get the result. Another solution is using http://phpjs.org/ library. Here you can find many functions available in JS as available in php. Usage is also same as that of PHP. So I feel if you get the json_encode/json_decode from there and use that then it can solve your problem easily.

Remember you can compile your needed functions only. In your case, it is json_encode and json_decode. No need to download whole library. Url to compile your library: http://phpjs.org/packages/configure

2 Comments

PHPJS's json_encode and _decode only works on JS objects, not on PHP equivalents.
Yes, Emil. You are right. I have not tested every function there but according to the way they are doing that json_encode/decode should work the same way as PHP counterpart is working. I have used few functions from there and that was working in correct way. Thanks for pointing the error.

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.