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

admin2025-06-05  2

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);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749114459a316485.html

最新回复(0)