最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Ajax random number always the same

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论