最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to retrieve data from database, then pass it to Javascript?

matteradmin9PV0评论

I'm trying to select a column from database like so:

global $wpdb;
$results = $wpdb->get_col("SELECT start_time FROM admin_form_entry");
$start = $results[0];

Then, I send data to external javascript file:

function load_js_assets() {
if( is_page( 13 ) ) {
    wp_enqueue_script('my-js', get_template_directory_uri() . '/testrun.js');
    wp_localize_script( 'my-js', 'passedObject', array(
            'start' => $start
            )
        );
    } 
}

add_action('wp_footer', 'load_js_assets');

And in the Javascript file the passedObject is always null!

var start = passedObject.start;
console.log("start >>> ", start);
Post a comment

comment list (0)

  1. No comments so far