0

I have my D3 code in main.js to load data from a PHP script get_data.php and display it using DataTables. After loading this initial table, all rows are assigned a class .data-cell, which I bind with a click-listener that loads a second table using another PHP script get_secondary_data.php.

$(document).ready(function () {

    d3.json('../php/get_data.php', function (data) {

        console.log(data);  // first console log output

        var rows = add_row(data);
        var cells = add_cell(rows); // draw cells and add class data-cell to each

        $('#data-table').DataTable();

        d3.selectAll('.data-cell').on('click', function (d) {
            // extract the data url
            console.log(d.data_url); // second console log output

            // draw secondary table
            load_secondary_table(d.data_url);

        });
    });
});

function load_secondary_table(data_url) {
    d3.json('../php/get_secondary_data.php', function (secondary_data) {
        console.log(secondary_data); // third console log output

        // drawing secondary table logic
    });
}

The first script loads the data fine, and I can see the output of 1st console log. The second console log is also fine, I can see the data_url of the correct row that I click. However, the third console log output secondary_data as null.

I have run the second PHP script by itself and it returns the correct result. I have also checked the Chrome's Network tab and did see the correct JSON output in the response of the second PHP script. It just not gets loaded into my page.

May I know what's wrong?

4
  • your parameter of load_secondary_table is not used. Does get_secondary_data.php require something? Commented May 4, 2020 at 17:29
  • @Bernhard yes, it's supposed to be a param for the PHP script (which I don't know how to pass it in from D3). However, I hard code some value in the script just to see if the result is loaded. Commented May 4, 2020 at 17:34
  • are you sure you return valid json? copy the result of the second script to a json validator Commented May 4, 2020 at 17:47
  • @Bernhard argh, I found the issue, in my PHP I also echo some database connection debug log while I should only echo the final JSON. Now it's fine. Thank you. Please put your suggestion as answer :) Commented May 4, 2020 at 17:59

1 Answer 1

1

are you sure you return valid json? Copy the result of the second script to a json validator

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

Comments

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.