Skip to content Skip to sidebar Skip to footer

Creating A 2-dimensional Jquery Array From A 2-dimensional Php Array With Ajax

I am using jQuery, AJAX and PHP to update the contents of a drop down box on an event. My code currently triggers the event and uses AJAX to call a PHP function which goes to the

Solution 1:

Have you looked at json_encode?

echo json_encode($outData);

This will convert it into a json object that can be read by jQuery.

Solution 2:

your looking for json

//php
echo json_encode($outData);

//javascript
$.ajax({
    type: "POST",
    url: "ops.php",
    data: "op=getInfo&" + data,
    dataType: "json",
    success: function(outData) {   
        console.log(outData); //this will be an object just like //your php associative array
    },
    error: function() {

    }
});

Solution 3:

JSON can do the trick, but why not look at it from another angle?

If you're pinging PHP to get updated info, just have PHP output the option values you want in your select box. Then use the HTML return of jQuery AJAX to .html() the result into your select element.

There's a couple of different ways to skin a cat, and I would submit that this much easier approach is going to gain you extra time to do more jQuery wizardry.

Solution 4:

jQuery can not read the echo of a PHP array. Use json_encode before you output it:

echo json_encode($outData);

That's a format jQuery actually can parse as the response.

Post a Comment for "Creating A 2-dimensional Jquery Array From A 2-dimensional Php Array With Ajax"