在开发WordPress插件时将AJAX与PHP连接的问题

时间:2017-04-28 14:48:21

标签: php ajax wordpress shortcode

所以基本上我正在尝试在WordPress中创建我的第一个插件,而不是非常熟悉PHP。我希望我的PHP脚本打印出一个零和三个按钮,其中一个加两个数字,一个加10,最后一个按钮除以2。尝试此操作后,我得到了500个HTML响应。能够打印零和按钮,但AJAX部分不起作用。

编辑:我试过删除“ajax_object.ajaxurl”周围的引号和

add_action('wp_ajax_change_number','erik_ajax_change_number'); add_action('wp_ajax_nopriv_change_number','erik_ajax_change_number');

add_action('wp_ajax_erik_change_number','erik_ajax_change_number'); add_action('wp_ajax_erik_nopriv_change_number','erik_ajax_change_number');

但这仍然无效

myfirstphp.php

<?php

$zero = 0;

//prefixed functions
function erik_print_zero_init(){
    function erik_print_zero(){
        global $zero;
        return $zero;
    }
    add_shortcode('zerohere', 'erik_print_zero');
}
add_action('init', 'erik_print_zero_init');

function erik_add_buttons_init(){
    function erik_add_buttons(){
        return '<button type="button" class="erik_button" name="erik_button1">+2</button>
                <button type="button" class="erik_button" name="erik_button2">+10</button> 
                <button type="button" class="erik_button" name="erik_button3">/2</button>';
    }
    add_shortcode('erik_buttons', 'erik_add_buttons');
}
add_action('init', 'erik_add_buttons_init');


add_action( 'wp_enqueue_scripts', 'so_enqueue_scripts' );
function so_enqueue_scripts(){
  wp_register_script( 'ajaxHandle', get_template_directory() . '/public_html/wp-content/plugins/myfirstphp/myajax.js', array(), false, true );
  wp_enqueue_script( 'ajaxHandle' );
  wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
}







add_action('wp_ajax_change_number', 'erik_ajax_change_number');
add_action('wp_ajax_nopriv_change_number', 'erik_ajax_change_number');


function erik_ajax_change_number($_POST['name']){
    if (isset($_POST['name']) && !empty($_POST['name'])) {
        switch ($_POST['name']) {
             $zero;
            case 'erik_button1':
                global  $zero +=2;
                break;
            case 'erik_button1':
                global  $zero +=10;
                break;
            case 'erik_button3':
                global  $zero /=2;
                break;
        }
    }
    wp_die();
}

?>

myajax.js

$(document).ready(function(){
    $('.erik_button').click(function(){
        var buttonName = $(this).attr('name');
        $.ajax({
        type: "POST",
        url: "ajax_object.ajaxurl",
        action: 'erik_ajax_change_number',
        data:{ name = buttonName},
        success: function(output){
            alert(output);
        }
        });
    });
});

有人能指出我在这里做错了什么或给我一些排除故障的提示吗?

1 个答案:

答案 0 :(得分:1)

您需要更改

add_action('wp_ajax_change_number', 'erik_ajax_change_number');
add_action('wp_ajax_nopriv_change_number', 'erik_ajax_change_number');

add_action('wp_ajax_erik_ajax_change_number', 'erik_ajax_change_number');
add_action('wp_ajax_nopriv_erik_ajax_change_number', 'erik_ajax_change_number');

点击此处https://codex.wordpress.org/AJAX_in_Plugins