admin管理员组

文章数量:1130349

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);

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);

本文标签: How to retrieve data from databasethen pass it to Javascript