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

1

1 Answer 1

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);
        }
    );
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi r3mus, thanks for comment but I am getting the data from MySQL database.
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.
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!

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.