0

Hi I'm working on passing back an array from php to javascript. I learned online that I should use json_encode on the array when passing it back but now that i have it in the ajax i'm unsure how i can loop over it because doing things like response[0] gives me [ and response[1] gives me " although when writing the entire thing to the document using innerHTML i can see it looks like an array but using a for loop gives me each letter like i stated above with the response[0] equaling [ rather than the first entry. What am i doing wrong? Any help is greatly appreciated!

PHP

<?PHP
    $link = mysql_connect('localhost', 'root', 'root');
    mysql_select_db("Colleges");
    $result = mysql_query("SELECT * FROM `Colleges` ORDER BY School");
    $schools = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($schools, $row['School']);
    }
    mysql_close();
    die(json_encode($schools));
?>

Ajax

<script type="text/javascript">
    function schools(){
        $.ajax({
            url: "Schools.php",
                type: "POST",
            success: function (response) {
                //Loop over response
            }
        });
    }
</script>

4 Answers 4

1

You should decode your JSON response (which is a string actually) to be able to work with it as with an object:

var respObj = JSON.parse(response);

The other way around is noticing jQuery that JSON will be supplied by the server (with either dataType: 'json' ajax parameter or Content-Type: application/json response header).

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

Comments

0

In the object you pass to the ajax method, you should try to add dataType: 'json' in order to specify that the result is json, or you could specify it in your php script calling header('Content-type: application/json'); before the call to die();

Doing so will result in your response being the object you expect instead of a string.

Finally, you could leave it as is, and call in your success callback response = $.parseJSON(response); which will take the response string and turn it into an object, see http://api.jquery.com/jQuery.parseJSON/

Comments

0
Use Following if it helps

 res=jQuery.parseJSON(response);

for(i=0;i<res.length;i++)
{
  alert(res[i].propertyname);

}

here property name implies to the keys on json .In your case it can be 'School' or just a number i or value can also be just res[i]

Comments

0

Javascript

for ( variable in response ) {
    alert(results[variable]);
}

JQuery

$.each(response, function(ind, val){
   alert("index:" + ind + ". value:" + val);
});

1 Comment

response supposed to be an array; and it's not recommended to loop through arrays with for-in. With jQuery, rather good $.each method is available; without, old crusty for (var i = 0, l < arr.length; i < l; i++) { ... } idiom is good enough.

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.