admin管理员组文章数量:1335349
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 );
});
});
本文标签: phpAjax random number always the same
版权声明:本文标题:php - Ajax random number always the same 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333941a2455246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论