0

I need to store some retrieved data from Database into an array. I know we can present the data as this example:

$('#loader').click(function () {
    $.get(
        'results.php', {
            id: $(this).val()
        },
        function (data) {
            $('#result').html(data);
        }
    );
});

but how I can store the function(data){} into an array like var datalist = []

Thanks

brandonscript
  • 68,675
  • 32
  • 163
  • 220
Behseini
  • 6,066
  • 23
  • 78
  • 125
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Barmar Dec 15 '13 at 08:01

1 Answers1

1

Assuming your incoming data is JSON, you can declare an object before calling your function, and then set data to datalist after the call completes:

var datalist = {};
$('#loader').click(function()
{
    $.get(
        'results.php', { 
            id : $(this).val() 
        },
        function(data) {
            datalist = JSON.parse(data);
        }
    );
});
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Hi r3mus, thanks for comment but I am getting the data from MySQL database. – Behseini Dec 15 '13 at 07:57
  • That wasn't what you originally asked, but you're not going to be able to call the MySQL database directly (major security risk notwithstanding). You need to build a php (de-facto standard) backend to handle the SQL queries, then output the data as JSON. Retrieve an associative array from your database, then use `json_encode` to convert the array to JSON. – brandonscript Dec 15 '13 at 08:00
  • Well I already have my PHP running and I think working fine! I am able to get the data from database and render them on #result. But I just need to save them inside an array instead of having them on the page! – Behseini Dec 15 '13 at 08:05