I have a button that when I click it should generate a random number using ajax ( It needs to be processed by PHP in real world application).
This works fine except that every time the button is clicked it produces the same random number. Why doesn't the ajax call run the php rand function new each time?
rand.php
function rand_load_scripts() {
//create an object that is accessible from our Javascript The name of this object is the second parameter, rand_script_vars
wp_enqueue_script('rand-script', plugin_dir_url( __FILE__ ) . 'assets/rand-script.js');
//an array of variables to pass to javascript.
wp_localize_script('rand-script', 'rand_script_vars', array(
'get_prices' => rand()
)
);
}
add_action('wp_enqueue_scripts', 'rand_load_scripts');
rand-script.js
jQuery(document).ready(function($) {
$('#rand_button').on('click', function() {
console.log( rand_script_vars.get_prices );
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742333941a4424271.html
评论列表(0条)