php - Ajax random number always the same

I have a button that when I click it should generate a random number using ajax ( It needs to be processed by PHP in rea

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

相关推荐

  • php - Ajax random number always the same

    I have a button that when I click it should generate a random number using ajax ( It needs to be processed by PHP in rea

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信