PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Retrieving JSON data from a MySQL Resultset using jQuery

Posted by teejay on November 7, 2009

This is a follow-up to the previous post, How to Use JSON with jQuery AJAX.

Retrieving JSON data from PHP and MySQL is so simple. It can be achieved by using the example in our previous post. However, what happens when we return a result set?

This wouldn't suffice.

$('document').ready(function(){
    $('#submit).click(function(){
        $.post('your_php_page.php', {employee_id: '456'},
            function(data){
                console.log(data.first_name);
                console.log(data.last_name);
            }, 'json');
    })
});

We will need to make some changes for it to change.

$('document').ready(function(){
    $('#submit).click(function(){
        $.post('your_php_page.php', {employee_id: '456'},
            function(data){
                for(i = 0;i < data.length; i++)
                {
                    console.log(data[i].first_name);
                    console.log(data[i].last_name);
                }
            }, 'json');
    })
});

Easy!

Note: Parsing data using jQuery isn't the safest way. See JSON and Browser Security

Categories: How To, Web Development

Tags: javascript, jquery, JSON

No Comments

Comments are not allowed